2014-08-30 17 views
0

在這個例子中,alert(selectedString)檢索下拉選擇值,所以我知道這部分工作。我想採取這個價值,並用它來查詢與mysqli和PHP表。我們如何使用選定的字符串查詢數據庫表?從onchange選定的值使用jquery/javascript的簡單查詢?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript"> 

var select = document.getElementById("test"); 

select.onchange = function(){ 
var selectedString = select.options[select.selectedIndex].value; 


alert(selectedString); 

} 

</script> 
+0

您需要使用AJAX調用php腳本並將選定的字符串作爲數據參數傳遞。然後,您可以回顯查詢的結果數組,然後使用成功回調中的結果更新您的Dom – Alex 2014-08-30 22:49:28

回答

0

使用jQuerys ajax method。在用戶在網站上時對您的服務器進行異步調用。在data屬性中傳遞selectedString並在$_GET['selectedString']的腳本中讀取它。在客戶端,您可能需要使用done塊中的php請求返回的數據。

$.ajax({ 
    url: "yourServerScript.php", 
    data: { selectedString: selectedString }, 
    cache: false 
}) 
    .done(function(html) { 
    $("#results").append(html); 
    });