2014-02-05 70 views
-2

我想獲得下面的AJAX腳本來傳遞一個下拉ID到PHP運行查詢,但它並沒有出現該變量實際上被傳遞。當我對PHP文件進行硬編碼時,查詢運行正常,但是當我嘗試動態執行查詢時,查詢返回「undefined」或根本沒有任何結果。AJAX沒有將變量傳遞給PHP的MySQL查詢

AJAX代碼

function ajax_post(){ 
var request = new XMLHttpRequest(); 
var id = document.getElementById("editorginfo").value; 
     alert (id); 
request.open("POST", "parse.php", true); 
request.setRequestHeader("Content-Type", "x-www-form-urlencoded"); 
request.onreadystatechange = function() { 
    if(request.readyState == 4 && request.status == 200) { 
     var return_data = request.responseText; 
     alert (return_data); 
     document.getElementById("orgeditname").value = return_data; 
     document.getElementById("orgeditphone").value = return_data;  
    } 
} 

request.send("id="+id); 
} 

PHP解析代碼

<?php 
include_once('../php_includes/db_connect.php'); 

$searchid = $_POST['id']; 

//$searchid = 1; 

$sql = 'SELECT * FROM orginfo WHERE id = $searchid'; 

$user_query = mysqli_query($db_connect, $sql) or die("Error: ".mysqli_error($db_connect)); 

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) { 

$orgid = $row["id"]; 

$orgname = $row["orgname"]; 

$orgphone = $row["orgphone"]; 

echo $orgname, $orgphone; 

} 
?> 

不能確定該信息所在的迷路。當我提醒編號時,它會捕獲正確的信息,所以我認爲問題出在我的發送部分,但我無法弄清楚我做錯了什麼。任何幫助,將不勝感激。

在此先感謝。

回答

1

您的請求標題不妥。更改此行 -

request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
              ^^^^^^^^^^^^