SQL查询将空结果返回给ajax函数

时间:2016-04-22 20:09:18

标签: javascript php jquery mysql ajax

我试图根据其他下拉列表的值来填充“Reigons”下拉列表,这是使用php和javascript的“城市”,代码确实工作时将附加区域名称和ID添加到select标签中它返回 尽管它们存在于数据库中! 注意:SQL查询没有问题,因为当我在wampserver

上执行它时,它可以正常工作

这是我的DB(HWP):

CREATE TABLE `cities` (
`city_id` int(5) NOT NULL auto_increment,
`city_name` varchar(50) NOT NULL, 
 PRIMARY KEY  (`city_id`),
 UNIQUE KEY `city_name` (`city_name`));

CREATE TABLE `reigons` (
`city_id` int(5) NOT NULL,
`reigon_id` int(5) NOT NULL auto_increment,
`reigon_name` varchar(50) NOT NULL,
 PRIMARY KEY  (`reigon_id`),
 UNIQUE KEY `reigons_name` (`reigon_name`));

 INSERT INTO `cities` (`city_id`, `city_name`) VALUES 
 (3, 'ALJAZERA'),
 (1, 'Khartoum'),
 (2, 'RED SEA');

 INSERT INTO `reigons` (`city_id`, `reigon_id`, `reigon_name`) VALUES 
(1, 1, 'Khartoum'),(1, 2, 'Bahri'),(1, 3, 'Um dorman'),
(2, 4, 'Read sea North'),(2, 5, 'Read sea South'),(3, 6, 'Baant'),
(3, 7, 'Soriba');

Home.php:

<?php
$con=mysql_connect("localhost", "root", ""); 
$db=mysql_select_db("hwp",$con);   
$query2 = mysql_query("select * from cities;") or die (mysql_error());?>
<select name="city" id="city" onchange="get_reg(this.value)">
    <option value="defult"> -Select City- </option>
    <?php
    while ($f2=mysql_fetch_array($query2)) {
    echo ('<option value="'.$f2['city_id'].'">'. $f2['city_name'].'</option>');}
    ?>
</select>

<script type="text/javascript">
   function get_reg(city)
   {
     $.ajax({
     type: "POST",
     url: 'getreg.php',
     data: city,
     success: function(html) {
       $("#reigon").html(html);
    }});
  }
</script>

<select name="reigon" id="reigon">  
    <option value="defult"> -Select Reig- </option>                     
</select>

getreg.php:

<?php 
mysql_connect("localhost","root","");
mysql_select_db("hwp");
$city= mysql_real_escape_string($_POST["city"]);
$query1 = mysql_query("select * from reigons where city_id= '$city'") or   die(mysql_error());
  while ($f1 = mysql_fetch_array($query1)) 
    {
        echo '<option value="', $f1['reigon_id'] ,'">', $f1['reigon_name'],'</option>';
    }
?>

请帮助。

0 个答案:

没有答案