您正在使用ajax处理国家/地区,州,城市的三重下拉列表,参考链接为:http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html。它成功地工作但我需要如果一个状态在db表中没有城市然后出现一个新的文本框,输入的值存储在php mysql中。什么编码我实现了。请提出一些想法。
代码:
的Ajax:
<script language="javascript" type="text/javascript">
function getXMLHTTP() {
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 getState(countryId) {
var strURL = "findState.php?country=" + countryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function () {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('statediv').innerHTML = req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getCity(countryId, stateId) {
var strURL = "findCity.php?country=" + countryId + "&state=" + stateId;
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>
形式:
<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="getState(this.value)">
<option value="">Select Country</option>
<option value="1">USA</option>
<option value="2">Canada</option>
</select></td>
</tr>
<tr style="">
<td>State</td>
<td ><div id="statediv"><select name="state" >
<option>Select Country First</option>
</select></div></td>
</tr>
<tr style="">
<td>City</td>
<td ><div id="citydiv"><select name="city">
<option>Select State First</option>
</select></div></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
findstate.php
<?php
include('config.php');
$country = intval($_GET['country']);
$query = "SELECT id,statename FROM state WHERE countryid='$country'";
$result = mysql_query($query);
?>
<select name="state" onchange="getCity(<?php echo $country?>,this.value)">
<option>Select State</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['statename']?></option>
<?php } ?>
</select>
findcity.php
<?php
include('config.php');
$countryId = intval($_GET['country']);
$stateId = intval($_GET['state']);
$query = "SELECT id,city FROM city WHERE stateid='$stateId'";
$result = mysql_query($query);
?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value><?php echo $row['city']?></option>
<?php } ?>
</select>
我想显示在任何州都没有城市,然后会出现一个文本框,它的值存储到db。
答案 0 :(得分:5)
将其放在 findcity.php
文件
<?php
include('config.php');
$countryId=intval($_GET['country']);
$stateId=intval($_GET['state']);
$query="SELECT id,city FROM city WHERE stateid='$stateId'";
$result=mysql_query($query);
if(mysql_num_rows($result) == 0) // no cities found
{
echo '<input type="textbox" name="city" />';
}
else // show select box
{
?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value><?php echo $row['city']?></option>
<?php } ?>
</select>
<?php
}
?>
答案 1 :(得分:0)
您的findstate.php
变成了这个
<?php
include('config.php');
$country=intval($_GET['country']);
$query="SELECT id,statename FROM state WHERE countryid='$country'";
$result=mysqli_query($query);
if(mysqli_num_rows($result)==0):?>
<input type="text" name="state" value="" />
<? endif; ?>
<select name="state" onchange="getCity(<?=$country ?>,this.value)">
<option>Select State</option>
<?php while($row=mysqli_fetch_array($result)): ?>
<option value=<?= $row['id']?>><?= $row['statename']?></option>
<?php endwhile; ?>
</select>
同样改变你的findcity.php
。
<强>建议强>
永远不要再使用mysql*
使用mysqli*
或PDO
,因为mysql*
已被删除。 Link