我想从php变量中的“list_cust_name”中选择项目,通过在WHERE子句中传递该php变量,通过该sql查询获取另一个下拉列表“list_cust_city”中的值。这是我的代码..请帮助我.. < / p>
<td width="228">
<label style="color:#000">Name </label>
<?php
$query_name = "SELECT DISTINCT cust_name FROM customer_db ORDER BY cust_name"; //Write a query
$data_name = mysql_query($query_name); //Execute the query
?>
<select id="list_cust_name" name="list_cust_name">
<?php
while($fetch_options_name = mysql_fetch_assoc($data_name)) { //Loop all the options retrieved from the query
?>
<option value="<?php echo $fetch_options_name['cust_name']; ?>"><?php echo $fetch_options_name['cust_name']; ?></option>
<?php
}
?>
</select>
</td>
<td width="250">
<label style="color:#000">City </label>
<?php
$query_city = "SELECT DISTINCT cust_city FROM customer_db ORDER BY cust_city"; //Write a query
$data_city = mysql_query($query_city); //Execute the query
?>
<select id="list_cust_city" name="list_cust_city">
<?php
while($fetch_options_city = mysql_fetch_assoc($data_city)) { //Loop all the options retrieved from the query
?>
<option value="<?php echo $fetch_options_city['cust_city']; ?>"><?php echo $fetch_options_city['cust_city']; ?></option>
<?php
}
?>
</select>
</td>
答案 0 :(得分:0)
您需要AJAX来电。
首先将JQuery
脚本包含在<head>
标记内的页面中,如:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
然后你需要一个事件监听器来更改你的第一个下拉列表,如下所示:
<script>
$('#list_cust_name').change(function(){
$.ajax({
url:'city.php',
data:{cust_name:$( this ).val()},
success: function( data ){
$('#list_cust_city').html( data );
}
});
});
</script>
将上述脚本放在<body>
标记下方。
现在,您要创建一个名为city.php
的页面,如上面的ajax调用所示。您可以根据需要决定名称。
然后在该页面中,您可以获取传递的值cust_name
,然后您可以执行查询。新的下拉选项可以生成如下。
<强> city.php 强>
<?php
$cust_name=$_GET['cust_name']; //passed value of cust_name
$query_city = "your query with the cust_name"; //Write a query
$data_city = mysql_query($query_city); //Execute the query
while($fetch_options_city = mysql_fetch_assoc($data_city)) { //Loop all the options retrieved from the query
?>
<option value="<?php echo $fetch_options_city['cust_city']; ?>"><?php echo $fetch_options_city['cust_city']; ?></option>
<?php
}
?>
完成此脚本后,数据将传递回AJAX调用,并根据我们的代码放在第二个下拉列表中。试一试
答案 1 :(得分:0)
就像Jokey说的那样,你需要在这里执行ajax ......
Upto选择客户名称没有问题。
你必须像这样在jquery(ajax)中捕获list_cust_name
的更改事件
JS文件:
$('#list_cust_name').change(function(){
var cust_name = $('#list_cust_name').val();
$.post(
"./php/getCityNames.php",
{ cust_name: cust_name },
function(data) {
$('#list_cust_city').html(data);
}
);
});
PHP文件(名为getCityNames.php):
<?php
$cust_name = $_POST["cust_name"]; //getting the customer name sent from JS file
$query_city = "SELECT DISTINCT cust_city FROM customer_db WHERE cust_name = '$cust_name' ORDER BY cust_city"; //added where as per your requirement
$data_city = mysql_query($query_city); //Execute the query
foreach($categories as $category){ //Its my style
echo "<option>";
echo $data_city['cust_city'];
echo "</option>";
}