[英]updating drop-down on change of another drop-down using php-ajax
I want to change the value of city drop-down on change of country drop-down. 我想更改国家下拉菜单中的城市下拉菜单的值。 Well now stuck at on point when I select the value in the country it says he variable $r is undefined.
现在,当我选择该国家/地区的值时,它说他的变量$ r是未定义的,因此停滞不前。 page : index.php
页面:index.php
<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="150">Country</td>
<td width="150"><select name="country" onChange="getCity('findcity.php?country='+this.value)">
<option value="">Select Country</option>
<option value="1">USA</option>
<option value="2">Canada</option>
</select></td>
</tr>
<tr style="">
<td>City</td>
<td ><div id="citydiv"><select name="city">
<option>Select City</option>
</select></div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
page: findcity.php 页面:findcity.php
<?php
$country=$_REQUEST['country'];
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="select city from city where countryid=$country";
$result=mysql_query($query);
?>
<select name="city">
<? while($r=mysql_fetch_array($result)) {
?>
<option value=""><?php echo $r['city'];?></option>
<? } ?>
</select>
here is the script 这是脚本
<script>
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCity(strURL) {
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
尝试使用mysql_fetch_assoc
代替mysql_fetch_array
您错过了在<?(write down php here) while($r=mysql_fetch_array($result)) {
附近写“ php” <?(write down php here) while($r=mysql_fetch_array($result)) {
“。并执行一些调试,例如检查您是否获得国家的ID,而不是检查查询是否返回正确的结果,或在浏览器上打印查询并在数据库上运行该查询。
change your code to.. 将您的代码更改为
<?php
while($r=mysql_fetch_array($result))
{
echo '<option value="">'.$r['city'].'</option>';
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.