首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果没有从下拉列表中选择值,则允许textbox使用php、mysql、javascript添加到mysql表。

如果没有从下拉列表中选择值,则允许textbox使用php、mysql、javascript添加到mysql表。
EN

Stack Overflow用户
提问于 2012-08-31 05:44:04
回答 2查看 2.6K关注 0票数 0

嗨,我正在为国家,州,城市使用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:

代码语言:javascript
运行
复制
<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>

表格:

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
<?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。

EN

回答 2

Stack Overflow用户

发布于 2012-08-31 05:49:49

把这个放在findcity.php文件中

代码语言:javascript
运行
复制
<?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
 }
 ?>
票数 5
EN

Stack Overflow用户

发布于 2012-08-31 05:53:27

你的findstate.php变成了

代码语言:javascript
运行
复制
<?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*被破坏了。链接

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12209524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档