<?php
include "config.php";
?>
<html>
<head>
<script type="text/javascript">
function agematch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('age').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'connection.inc.php?name='+document.ajax.name.value, true );
xmlhttp.send();
}
function countrymatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('country').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'country.inc.php?age='+document.ajax.age.value, true );
xmlhttp.send();
}
</script>
</head>
<body>
<form id="ajax" name="ajax" >
Choose your name : <select name="name" id="name" select="selected" onchange="agematch();"> <option> </option>
<?php
$query = "SELECT DISTINCT name FROM info";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo"<option value ='".$row[0]."'> '".@$row[0]."'</option>";
}
?>
</select>
Age : <select id="age" name="age" onchange="countrymatch();"> </select>
country : <select id="country" name="country"> <option> </option> </select>
</form>
</body>
</html>
现在,第一个Ajax调用的页面: -
<?php
include "config.php";
echo " <option> </option> " ;
if(isset( $_GET['name']) ) {
@$name = $_GET['name'];
}
$query = "SELECT age FROM info WHERE name = '".@$name."' ";
$result = mysql_query($query);
while ($query_row = mysql_fetch_array($result)) {
echo " <option value ='".$query_row[0]."'> $query_row[0]</option> ";
}
?>
现在,使用第三个下拉菜单的第二个Ajax调用页面: -
<?php
include "config.php";
if (isset( $_GET['age']) ) {
@$age=$_GET['age'];
}
$query = "SELECT country FROM info WHERE name='".@$name."' AND age='".@$age."' ";
$result= mysql_query($query);
while ($query_row = mysql_fetch_array($result)) {
echo " <option value = '".$query_row[0]."'> $query_row[0] </option> ";
}
?>
所以如你所见,这是代码,当然我通过一个名为“config.php”的页面连接到数据库,所以我希望你帮我解决这个问题并从数据库中检索数据第三个下拉列表“国家”。在此先感谢!
答案 0 :(得分:1)
您没有在第二次通话中传递name
。
替换
xmlhttp.open('GET', 'country.inc.php?age='+document.ajax.age.value, true );
带
xmlhttp.open('GET', 'country.inc.php?age='+document.ajax.age.value+'&name='+document.ajax.name.value, true );
希望有所帮助。