2014-03-26 22 views
0

我有一個窗體,用戶必須選擇第一個<select>,它會過濾數據庫上的第二個<select>並剛剛顯示給用戶。篩選器<select>與其他<select>與數據庫mysql和php

我已經看到了一些網站,我想將它放在我的

第一<select>

<select name="area" id="area"> 
    <option title="" value="">Select ...</option> 
    <option value="1">First</option> 
    <option value="2">Second</option> 
    <option value="3">third</option> 
</select> 

如果我選擇用價值1 O選項,它會在MySQL上搜索所有ID爲線= 1

SELECT * FROM tblarea WHERE id_prof = 1; 

而這種結果將使第二選擇

<select name="prof" id="prof"> 
    <option title="" value="">Select ...</option> 
    <option value="1">1.1</option> 
    <option value="2">1.2</option> 
    <option value="3">1.3</option> 
</select> 

雖然查詢工作,它會顯示在<select>「搜索...」

如果我想錯了,請幫助我,並給我出出主意。

感謝

+0

這不是真的清楚你的問題是 – Idris

+0

使用jquery ajax功能 – dontHaveName

+0

這是一個求職網站,所以我需要過濾,該地區和後顯示角色 – Guhh

回答

1

由於@dontHaveName建議,使用Ajax調用,這將是這個樣子:

$('#area').on('change', function(e) { 

    $.ajax({ 
    url: '/jobs/options/', 
    type: 'POST', 
    data: $(e.target).val(), 
    dataType: 'html', 
    success: function(opts) { 
     $('#prof').html(opts); 
    }, 
    error: function(xhr, status, err) { 
    console.log(err); 
    } 
    }); 

}); 

一些Ajax代碼是從內存中,所以讓我知道如果你得到錯誤。

+0

謝謝!謝謝!謝謝!它工作完美。 – Guhh

0

如果您的選擇對象上沒有任何項目,我建議您將所有可能性加載到JavaScript哈希中。在這種情況下,使用您的PHP程序來創建並填充此散列。一旦用戶改變第一選擇要素,你調用一個函數來填充第二選擇,使​​用這樣的事情:

<html> 
<body> 

<script language=JavaScript> 

//new hash is created 
var area_prof = new Object(); 

// hash is being populated 
area_prof["f"] = new Array("1.1","1.2","orange"); 
area_prof["s"] = new Array("2.1","2.2","white"); 
area_prof["t"] = new Array("3.1","3.2","3.3","hello world"); 

function populate() 
{ 
var value=document.getElementById("area").options[document.getElementById("area").selectedIndex].value; 

while (document.getElementById("prof").options[1]) 
    { 
    document.getElementById("prof").options[1] = null; 
    } 

for (var i=0; i<area_prof[value].length; i++) 
    { 
    document.getElementById("prof").options[i+1] = new Option(area_prof[value][i],i); 
    } 
} 

</script> 

<select name="area" id="area" onChange=populate()> 
    <option title="" value="">Select ...</option> 
    <option value="f">First</option> 
    <option value="s">Second</option> 
    <option value="t">third</option> 
</select> 

<select name="prof" id="prof"> 
    <option title="" value="">Select ...</option> 
</select> 

</body> 
</html> 

但如果你真的想/需要創建另一個HTTP請求到加載第二個組合框, AJAX是你所需要的:

https://api.jquery.com/jQuery.ajax/

最好的問候,

愛德華多·馬亞

+0

不幸的是,我無法加載所有內容,因爲它太重了。但非常感謝你! – Guhh