0

嗨,我正在使用 ajax 为国家、州、城市开发三重下拉列表,参考链接是:http ://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-来自-database-using-ajax-and-php.html 的值。它成功工作,但我需要如果一个州在 db 表中没有城市,那么会出现一个新的文本框,并且输入的值存储在 php mysql 中。我正在实现什么编码。请给出一些想法。

代码:

阿贾克斯:

<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>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</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。

4

2 回答 2

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
 }
 ?>
于 2012-08-31T05:49:49.617 回答
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*关联

于 2012-08-31T05:53:27.603 回答