<?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”的页面连接到数据库,所以我希望你帮我解决这个问题并从数据库中检索数据第三个下拉列表“国家”。 在此先感谢!
好的,Musa在这里是编辑: -
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;
}
}
var age = encodeUriComponent(document.ajax.age.value),
var name = encodeUriComponent(document.ajax.name.value),
xmlhttp.open('GET', 'country.inc.php?age='+age+'&name'+name, true );
xmlhttp.send();
}
还有: -
<?php
include "config.php";
if (isset($_GET['age'], $_GET['name']) ) {
@$age=$_GET['age'];
@$name = $_GET['name'];
}
$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> ";
}
?>
我没有收到任何错误消息,我相信你的观点是正确的,但不幸的是这个解决方案无效!非常感谢你帮助我:)。
答案 0 :(得分:1)
您没有在第二个ajax请求中发送该名称,但是您需要它来进行数据库查询,因此您需要在ajax请求中发送名称和年龄。你也没有消毒你的输入,你必须总是验证用户输入,我还建议你不要使用mysql _ *
自PHP 5.5.0起,此扩展程序已弃用,将来将被删除。相反,应使用MySQLi或PDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API指南和related FAQ。
var age = encodeURIComponent(document.ajax.age.value),
var name = encodeURIComponent(document.ajax.name.value),
xmlhttp.open('GET', 'country.inc.php?age='+age+'&name'+name, true );
if (isset($_GET['age'], $_GET['name']) ) {
$age = $_GET['age'];
$name = $_GET['name'];
...
}