我的程序中有两个下拉菜单,我希望它是这样的:
我认为第一个下拉列表是正确的,但我希望得到它的值,并将其放在我的第二个下拉列表中的WHERE子句的SQL语句中。怎么样?
<tr>
<td><label for="cname">Client Name:</label></td>
<td><select name="cname" id="cname">
<option>Choose</option>
<?php
include("alqdb.php");
$result=mysqli_query($con, "SELECT ClientName FROM events");
while($row=mysqli_fetch_assoc($result)){
echo "<option>".$row["ClientName"]."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><label for="survey">Survey:</label></td>
<td><select name="survey" id="survey">
<option>Choose</option>
<?php
include("alqdb.php");
$result=mysqli_query($con, "SELECT EventTitle FROM events WHERE ClientName = 'Francis'");
while($row=mysqli_fetch_assoc($result)){
echo "<option>".$row["EventTitle"]."</option>";
}
?>
</select>
</td>
</tr>
答案 0 :(得分:2)
为第二个选择执行ajax请求:
为ajax请求创建一个php文件 HTML:
<tr>
<td><label for="cname">Client Name:</label></td>
<td><select name="cname" id="cname">
<option>Choose</option>
<?php
include("alqdb.php");
$result=mysqli_query($con, "SELECT ClientName FROM events");
while($row=mysqli_fetch_assoc($result)){
echo "<option>".$row["ClientName"]."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><label for="survey">Survey:</label></td>
<td><select name="survey" id="survey">
<option>Choose</option>
</select>
</td>
</tr>
ajax.php
<?php
include("alqdb.php");
$result=mysqli_query($con, "SELECT EventTitle FROM events WHERE ClientName = '"$_GET['cname']"'");
while($row=mysqli_fetch_assoc($result)){
echo "<option>".$row["EventTitle"]."</option>";
}
?>
JS:
<script>
$(function(){
$('#cname').on('change',function(){
$.ajax({
url:ajax.php,
data:{cname:$('#cname').val()};
type:'get',
contentType:'html',
success:function(data){
$('#survey').append(data);
}
});
});
});
</script>