小编典典

级联下拉jquery ajax php

ajax

我有以下代码以生成级联下拉列表,但是由于某种原因,ajax发布无法正常工作。我可以获取它以填充状态列表,当我选择一个状态时,我会向我显示正确的值,但是当需要发布到fetch_state.php时,它似乎发布了null。有人可以帮我解释为什么这样做吗?

这是下面的代码

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

阅读 214

收藏
2020-07-26

共1个答案

小编典典

首先,将您更改mysql_escape_stringmysqli_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>

2020-07-26