级联下拉列表jquery ajax php

时间:2014-08-01 18:50:28

标签: php jquery ajax

我有以下代码以生成级联下拉列表,但由于某种原因,ajax帖子无效。我可以让它填充状态列表,当我选择一个状态时,我会提醒我显示正确的值,但是当发布到fetch_state.php的时候,它似乎是空的。有人可以帮我解释为什么要这样做吗?

以下是代码

的index.php

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
include("connection.php");
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body>
<div id="container">
  <div id="body">
    <div id="dropdowns">
       <div id="center" class="cascade">
          <?php
        $sql = "SELECT DISTINCT state FROM tbl_zip ORDER BY state ASC";
        $query = mysqli_query($con, $sql);
        ?>
            <label>State:
            <select name="state" id = "state">
              <option value="">Please Select</option>
              <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC )) { ?>
              <option value="<?php echo $rs["state"]; ?>"><?php echo $rs["state"]; ?></option>
              <?php } ?>
            </select>
            </label>
          </div>


          <div id="city" class="cascade"></div>
          <div id="zip" class="cascade"></div>        
        </div>
    </div>
  </div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select#state").change(function(){

    var state =  $("select#state option:selected").attr('value'); 
    alert(state);   
    $("#city").html( "" );
    $("#zip").html( "" );
    if (state.length > 0 ) { 
        alert(state.length);
     $.ajax({
            type: "POST",
            url: "fetch_state.php",
            data: "state="+state,
            cache: false,
            beforeSend: function () { 
                $('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#city").html( html );
            }
        });
    } 
});
});
</script>
</body>
</html>

fetch_state.php

<?php

include("connection.php");
var_dump($_POST);
$state = trim(mysql_escape_string($_POST["state"]));

$sql = "SELECT DISTINCT city FROM tbl_zip WHERE state = ".$state ." ORDER BY city";
$count = mysqli_num_rows( mysqli_query($con, $sql) );
if ($count > 0 ) {
$query = mysqli_query($con, $sql);
?>
<label>City: 
<select name="city" id="drop2">
    <option value="">Please Select</option>
    <?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
    <option value="<?php echo $rs["city"]; ?>"><?php echo $rs["city"]; ?></option>
    <?php } ?>
</select>
</label>
<?php 
    }

?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("select#drop2").change(function(){

    var state_id = $("select#drop2 option:selected").attr('value');
   // alert(state_id);
    if (state_id.length > 0 ) { 
     $.ajax({
            type: "POST",
            url: "fetch_city.php",
            data: "city="+city,
            cache: false,
            beforeSend: function () { 
                $('#city').html('<img src="loader.gif" alt="" width="24" height="24">');
            },
            success: function(html) {    
                $("#city").html( html );
            }
        });
    } else {
        $("#city").html( "" );
    }
});

});
</script>

我一直收到错误

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean 

1 个答案:

答案 0 :(得分:2)

首先,将您的mysql_escape_string更改为mysqli_escape_string

$state = trim(mysqli_escape_string($con, $_POST["state"]));

然后将state包装在引号中

$sql = "SELECT DISTINCT city FROM tbl_zip WHERE state = '".$state ."' ORDER BY city";

另外,从<script>中取出fetch_state.php块,然后将index.php块放在另一个<script>块中