通过传递选定的下拉列表值来获取第三个下拉列表的结果

get the result for third dropdown by passing the selected dropdown values

我正在显示基于上述选定下拉菜单的下拉菜单。我想要第三个下拉列表中的结果。为此,我正在 php 中编写 sql 查询并在 jquery 中编写更改事件,但我无法获得结果。我被困在那里

我的jquery长得像

$(document).ready(function(){
    $("#parent_cat,#city").change(function(){
        $.get('loadlocation.php?city=' + $(this).val() , function(data) {
          $("#sub_cat").html(data);

        });
    });
});

parent_cat 和城市来自所选值

<label for="category">Category</label>
<select name="parent_cat" id="parent_cat">
    <?php while($row = mysql_fetch_array($query_parent)): ?>
        <option value="<?php echo $row['name']; ?>"><?php echo $row['name']; ?></option>
    <?php endwhile; ?>
</select>
<br/><br/>
<label for="city">city</label>
<select name="city" id="city">
    <?php while($row = mysql_fetch_array($query_parent1)): ?>
        <option value="<?php echo $row['city']; ?>"><?php echo $row['city']; ?></option>
    <?php endwhile; ?>
</select>
<br/><br/>

我的 php 文件 loadlocation.php 是

<?php 
    include('config.php');
    $parent_cat = $_GET['parent_cat'];
    $city = $_GET['city'];
    $query = mysql_query("SELECT  table_place_detail.post_title FROM table_terms, table_place_detail, table_post_locations
    WHERE  table_place_detail.post_location_id =    table_post_locations.location_id AND  table_place_detail.default_category =  table_terms.term_id AND  table_post_locations.city =  '$city' AND table_terms.name =  '$parent_cat'");
    while($row = mysql_fetch_array($query)) {
        echo "<option value='$row[post_title]'>$row[post_title]</option>";
    }
?>

我想获取 parent_cat 的值,城市到 loadlocation.php,但我无法获取这些值。我想加载这两个值并执行查询,这些值应该显示在第三个下拉列表中,如下所示,任何人都可以帮助解决这个问题

<label>Vendors List 1</label>
<select name="sub_cat" id="sub_cat"></select>

有两点很突出

  • 您只发送了一个值,?city=
  • 根据手册jQuery.get(),您可以将附加参数作为普通对象发送。这意味着,您不需要构建查询字符串,但可以分别传递 parent_catcity,例如

    $.get("loadlocation.php",
          { parent_cat: $('#parent_cat').val(), city: $('#city').val() },
          function(data) {
              $('#sub_cat').html(data);
          });
    

最后,每个 mysql_* 页的强制提示

Warning This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

  • mysqli_query()
  • PDO::query()