這裏我試圖從數據庫中提取數據到其中我有10個表。我有兩個組合框,在第一個框中,我從數據庫中調用了選項,這將是第一個表格,以及像a,b,c,d這樣的選項。現在,當我在第一個框中選擇'a'時,它應該通過php調用相應的值,並且該值將來自另一個表,並且這兩個表中都沒有任何公用鍵。 與選擇'b'時相同,它將調用第三個表中的另一個值。如何將多個表的數據庫值存入動態選擇框
所以,我在這裏嘗試了if if條件,但我能夠得到輸出。 有人請提高jquery的錯誤,以哪種方式我應該匹配它的條件。
這裏是我的代碼的HTML頁面
<html>
<body>
<select id="a1_title">
</select>
<select id="a2_title">
</select>
<script src="jquery-2.0.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON("drpdwn.php", success = function(data){
var items="";
for(var i = 0; i< data.length; i++){
items +="<option value='"+ data[i].toLowerCase() +"'>" + data[i] +"</option>";
}
$("#a1_title").append(items);
$("#a1_title").change();
});
$("#a1_title").change(function(){
if(data.length == 1) // here i have tried to match the condition from ID but it doesn't work
{
alert("b");
$.getJSON("lulc_db.php",success = function(data){
alert("okay");
var items="";
for(var i = 0; i< data.length; i++){
items+="<option value='"+data[i].toLowerCase()+"'>"+data[i]+"</option>";
}
else if(data.length == 2)
{
alert("b");
$.getJSON("soil_db.php",success = function(data){
alert("okay");
var items="";
for(var i = 0; i< data.length; i++){
items+="<option value='"+data[i].toLowerCase()+"'>"+data[i]+"</option>";
}
$("#a2_title").append(items);
});
}
});
});
</script>
</body></html>
這是drpdown.php頁。這頁值將在第一個選擇框中成功的到來。
<?php
$host = "localhost";
$user = "postgres";
$pass = "admin";
$db = "Querybuilderdb";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
or die ("Could not connect to server\n");
$query = "SELECT id, name FROM tab";
$result = pg_query($con, $query);
if(pg_num_rows($result)){
$data=array();
while($row=pg_fetch_array($result))
{ array_push($data, $row["name"]);
}
echo json_encode($data);
}
?>
這裏lulc_db.php。這頁值應排在第二選擇box.Same這樣我有8其他PHP頁面,我已經表明進入第二個下拉框。
<?php
require "config.php";
$query = "SELECT distinct level_1 FROM pachgaon_LULC";
$result = pg_query($con, $query);
if(pg_num_rows($result)){
$data=array();
while($row=pg_fetch_array($result))
{ array_push($data, $row["level_1"]);
}
echo json_encode($data);
}
?>
在先進的感謝:)
您可能需要嵌套的'for'循環。看到這個[post](http://stackoverflow.com/questions/800593/loop-through-json-object-list)通過json循環。考慮js對象中的鍵/值對:'$ .each(data,function(key,val){...})' – Parfait
我不能應用循環,因爲我想要的結果只有通過選擇第一個框然後它會調用php頁面,並將結果提取到第二個組合框中。 – ads
從這個問題的答案中得到一些想法。 http://stackoverflow.com/questions/11451150/create-dynamic-combobox-using-php-and-onselection-change-values-of-another-dynam .hope這有助於.. :) – aimme