0
的index.php顯示級聯下拉列表
<div id="container">
<div id="body">
<div id="dropdowns">
<div id="center" class="cascade">
<?php
$sql = "SELECT searchname FROM search_parent";
$query = mysqli_query($con, $sql);
?>
<label>First Option:
<select name="searchname" id = "drop1">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["id"]; ?>"><?php echo $rs["searchname"]; ?></option>
<?php } ?>
</select>
</label>
</div>
<div class="cascade" id="state"></div>
<div id="city" 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#drop1").change(function(){
var country_id = $("select#drop1 option:selected").attr('value');
// alert(country_id);
$("#state").html("");
$("#city").html("");
if (country_id.length > 0) {
$.ajax({
type: "POST",
url: "fetch_state.php",
data: "country_id="+country_id,
cache: false,
beforeSend: function() {
$('#state').html('<img src="loader.gif" alt="" width="24" height="24">');
},
success: function(html) {
$("#state").html(html);
}
});
}
});
});
</script>
fetch_state.php
<?php
include("connection.php");
$country_id = trim(mysqli_escape_string($con,$_POST["country_id"]));
$sql = "SELECT * FROM t1 WHERE parent_id = '".$country_id ."' ";
$count = mysqli_num_rows(mysqli_query($con, $sql));
if ($count > 0) {
$query = mysqli_query($con, $sql);
?>
<label>Select:
<select name="state" id="drop2">
<option value="">Please Select</option>
<?php while ($rs = mysqli_fetch_array($query, MYSQLI_ASSOC)) { ?>
<option value="<?php echo $rs["id"]; ?>"><?php echo $rs["title"]; ?></option>
<?php } ?>
</select>
</label>
<?php
}
?>
<script src="jquery-1.9.0.min.js"></script>
表爲search_parent
id searchname
1 t1
2 t2
3 t3
當用戶選擇T1第二下拉列表中的值應該從t1表中填充,如果選擇了t2,它應該從t2表填充,並且如果t3被選擇了第二個下拉列表應該從T3表獲取填充
表T1
id title parent_id
1 tt1 1
然而
當我運行這段代碼,它不工作,它只是展示了第一個下拉列表,當我選擇一個值沒有被顯示進一步
'error_log'?任何控制檯錯誤? – mudasobwa 2014-12-03 08:29:33
@mudasobwa我越來越錯誤mysqli_escape_string()期望正好2個參數, – Sam 2014-12-03 09:10:31
也許它是有道理的傳遞正好兩個參數調用'mysqli_escape_string()'然後呢? – mudasobwa 2014-12-03 09:11:15