gpt4 book ai didi

php - 使用 Ajax 和 3 个连续的可靠下拉列表

转载 作者:行者123 更新时间:2023-11-29 14:04:40 24 4
gpt4 key购买 nike

愿大家平安,我是使用 Ajax 的新手,问题是,我有 3 个连接到数据库的下拉列表,第一个是“姓名”,第二个是“年龄”,第三个是一是“国”!因此,我已连接到数据库,并在第一个列表“名称”中检索数据,然后使用 Ajax,在选择第一个列表的任何选项后成功检索匹配数据并将它们放入名为“年龄”的第二个列表中,问题是,当我使用与第二个名为“age”的列表完全相同的方式将匹配数据检索到名为“country”的第三个列表时,它不起作用!所以请帮助我,因为我正在使用这个示例来学习 Ajax,然后应用于更大的实际项目!这是代码:- 首先是 home.php 页面:-

<?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”的页面连接到数据库的,所以我希望你帮助我解决这个问题并将数据从数据库检索到第三个下拉列表“国家”。提前致谢!

最佳答案

您没有在第二次调用中传递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 );

希望有帮助。

关于php - 使用 Ajax 和 3 个连续的可靠下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14560869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com