Php 依赖于jQuery下拉列表

Php 依赖于jQuery下拉列表,php,javascript,jquery,Php,Javascript,Jquery,我有两个下拉列表。第一个显示一个国家的地区,另一个显示所选州的每个城市。问题是在我提交表单MySQL数据库之后 从第一个列表获取所选区域的ID,而第二个列表不显示任何内容。我想让我的数据库接收正确的信息。地区名称和城市名称 我该怎么做 我的javascript如下所示: 我的section.php脚本是: 代码运行良好。但我不知道该怎么处理数据库。我想我必须更改JavaScript中的某些内容 需要帮忙吗 <script type="text/javascript"> $(docum

我有两个下拉列表。第一个显示一个国家的地区,另一个显示所选州的每个城市。问题是在我提交表单MySQL数据库之后 从第一个列表获取所选区域的ID,而第二个列表不显示任何内容。我想让我的数据库接收正确的信息。地区名称和城市名称

我该怎么做

我的javascript如下所示: 我的section.php脚本是: 代码运行良好。但我不知道该怎么处理数据库。我想我必须更改JavaScript中的某些内容

需要帮忙吗

<script type="text/javascript">
$(document).ready(function()
{
    $(".country").change(function()
    {
        var id=$(this).val(); 
        var region = $(this).find('option:selected').html();  
        $.ajax({
          type: "POST",
          url: "ajax_city.php",
          data: { country: id , name : region },
          cache: false,
          success: function(html)
           {
             $(".city").html(html);
             $(".city").removeAttr('disabled');
           } 
    });
    });
});
</script>
ajax_city.php

<?php
  if($_POST['country'])
{
    $conn=mysql_connect("localhost","root","");
    $db=mysql_select_db("country",$conn);


    $id = $_POST['country'];
    $region = $_POST['name'];
    $sql = mysql_query("SELECT DISTINCT city FROM CITY where pid = $id order by city");

    while($row = mysql_fetch_array($sql))
    {
        $id=$row['id'];
        $data=$row['city'];
        echo "<option value=$id>$data</option>";
    }
}

?>
数据库:国家

default.php上的Javascript

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

     <script type="text/javascript">
     $(document).ready(function()
 {
$(".country").change(function()
{
    var id=$(this).val(); 
    var region = $(this).find('option:selected').html();  
    $.ajax({
      type: "POST",
      url: "ajax_city.php",
      data: { country: id , name : region },
      cache: false,
      success: function(html)
       {
         $(".city").html(html);
         $(".city").removeAttr('disabled');
       } 
   });
  });
 });
  </script>

 <label>Country :</label> 
 <select name="country" class="country">
  <option selected="selected">--Select Region--</option>
     <?php
            $conn=mysql_connect("localhost","root","");
            $db=mysql_select_db("country",$conn);

        $sql=mysql_query("SELECT id,region FROM regions ORDER BY region");
        while($row = mysql_fetch_array($sql))
        {
            $id=$row['id'];
            $region=$row['region'];
            echo '<option value="'.$id.'">'.$region.'</option>';
        } 
        ?>
   </select><br/><br/>
   <label>City :</label> 
  <select disabled name="city" class="city">
     <option selected="selected">--Select City--</option>
  </select> 
ajax_city.php

<?php
  if($_POST['country'])
{
    $conn=mysql_connect("localhost","root","");
    $db=mysql_select_db("country",$conn);


    $id = $_POST['country'];
    $region = $_POST['name'];
    $sql = mysql_query("SELECT DISTINCT city FROM CITY where pid = $id order by city");

    while($row = mysql_fetch_array($sql))
    {
        $id=$row['id'];
        $data=$row['city'];
        echo "<option value=$id>$data</option>";
    }
}

?>

在ajax\u city.php中将$\u POST['id']替换为$\u POST['country'],选择与tag name属性键相同的tags value posts,并将$.region更改为$.country,你的html代码中没有.region元素我已经替换了你告诉我的所有内容,但是现在出现了另一个问题…现在在我的表单中,当我选择一个区域时,它不会显示我的任何城市…我不知道你编写的javascript是否正常工作。。。问题是,在我的数据库中,我有一个名为REGIONS的表,每个区域的ID从1到43,我想还有一个名为CITY的表,ID为CITY,CITY和CITY,其中属于某个特定区域的所有城市都有该区域的dame ID。现在,你告诉我用$u POST['country']替换$u POST['ID']…但是现在列表之间似乎没有任何联系…你能在这里给出你的表格结构吗?我想这可能有用…理解我想告诉你的内容。。。为什么要用$\u POST['country']替换$\u POST['id']。。。这如何将区域索引与城市索引相匹配???是否应该在脚本第一节中通过ID连接?将代码数据:dataString更改为数据:{country:ID},键为country,并删除行var dataString='ID='+ID;我对你的问题的第一个评论有错误,在你的第一个代码中,$\u post['id']是正确的
<label>Country :</label> 
<select name="country" class="country">
    <option selected="selected">--Select Region--</option>
        <?php
            $sql=mysql_query("SELECT id,region FROM regions ORDER BY region");
            while($row = mysql_fetch_array($sql))
            {
                $id=$row['id'];
                $region=$row['region'];
                echo '<option value="'.$id.'">'.$region.'</option>';
            } 
            ?>
</select><br/><br/>
<label>City :</label> 
<select disabled name="city" class="city">
    <option selected="selected">--Select City--</option>
</select>
<?php
    if($_POST['country'])
    {
        $id = $_POST['country'];
        $region = $_POST['name'];
        $sql = mysql_query("SELECT DISTINCT city FROM CITY where id_city = $id order by city");

        while($row = mysql_fetch_array($sql))
        {
            $id=$row['id'];
            $data=$row['city'];
            echo "<option value=$id>$data</option>";
        }
    }
?>
CREATE TABLE IF NOT EXISTS `city` ( `id_city` int(11) NOT NULL AUTO_INCREMENT,  `city` varchar(30) NOT NULL,  `pid` int(11) NOT NULL,  PRIMARY KEY (`id_city`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;


INSERT INTO `city` (`id_city`, `city`, `pid`) VALUES(1, 'Gujarat', 1),(2, Maharashtra', 1),(3, 'Rajasthan', 1),(4, 'Madhya Pradesh', 1),(5, 'Lahore', 2),(6, 'hyderabad', 2);

CREATE TABLE IF NOT EXISTS `regions` ( `id` int(11) NOT NULL AUTO_INCREMENT,  `region` varchar(30) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `regions` (`id`, `region`) VALUES(1, 'India'),(2, 'Pakistan');
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

     <script type="text/javascript">
     $(document).ready(function()
 {
$(".country").change(function()
{
    var id=$(this).val(); 
    var region = $(this).find('option:selected').html();  
    $.ajax({
      type: "POST",
      url: "ajax_city.php",
      data: { country: id , name : region },
      cache: false,
      success: function(html)
       {
         $(".city").html(html);
         $(".city").removeAttr('disabled');
       } 
   });
  });
 });
  </script>

 <label>Country :</label> 
 <select name="country" class="country">
  <option selected="selected">--Select Region--</option>
     <?php
            $conn=mysql_connect("localhost","root","");
            $db=mysql_select_db("country",$conn);

        $sql=mysql_query("SELECT id,region FROM regions ORDER BY region");
        while($row = mysql_fetch_array($sql))
        {
            $id=$row['id'];
            $region=$row['region'];
            echo '<option value="'.$id.'">'.$region.'</option>';
        } 
        ?>
   </select><br/><br/>
   <label>City :</label> 
  <select disabled name="city" class="city">
     <option selected="selected">--Select City--</option>
  </select> 
<?php
  if($_POST['country'])
{
    $conn=mysql_connect("localhost","root","");
    $db=mysql_select_db("country",$conn);


    $id = $_POST['country'];
    $region = $_POST['name'];
    $sql = mysql_query("SELECT DISTINCT city FROM CITY where pid = $id order by city");

    while($row = mysql_fetch_array($sql))
    {
        $id=$row['id'];
        $data=$row['city'];
        echo "<option value=$id>$data</option>";
    }
}

?>