级联下拉列表jquery ajax php
我有以下代码,以生成级联下拉列表但由于某种原因,ajaxpost不起作用。 我可以让它填充状态列表,当我选择一个状态时,我会提醒我显示正确的值,但是当发布到fetch_state.php时,它似乎是空的。 有人可以帮助我为什么这样做?
这是下面的代码
的index.php
$(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(''); }, success: function(html) { $("#city").html( html ); } }); } }); });
fetch_state.php
0 ) { $query = mysqli_query($con, $sql); ?> $(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(''); }, success: function(html) { $("#city").html( html ); } }); } else { $("#city").html( "" ); } }); });
我一直收到错误
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
首先,将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";
另外,从fetch_state.php
取出块,并将其与
index.php
一起放在另一个块中