我正在显示基于上述选定下拉菜单的下拉菜单。我想要第三个下拉列表中的结果。为此,我在 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 和 city 来自选定的值
<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、city 的值到 loadlocation.php,但我无法获取这些值。我想加载这两个值并执行查询,这些值应该显示在第三个下拉列表中,如下所示,任何人都可以帮助解决这个问题
<label>Vendors List 1</label>
<select name="sub_cat" id="sub_cat"></select>
最佳答案
有两点很突出
- 您只发送一个值,
?city=
根据手册
jQuery.get()
,您可以将附加参数作为普通对象发送。这意味着,您不需要构建查询字符串,但可以分别传递parent_cat
和city
,例如$.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()
关于javascript - 通过传递选定的下拉列表值来获取第三个下拉列表的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093774/