2013-02-26 76 views
1

我目前正在學習AJAX,並且遇到了此錯誤,其中未顯示MySQL查詢的結果。嘗試打印出MySQL時出現AJAX問題查詢

下面的代碼片斷是JavaScript的:

<script type="text/javascript"> 
function showCustomers() 
{ 
    var zip = document.getElementById('zipcode').value; 
    var st = document.getElementById('stname').value; 
    if ((zip=="")&&(st=="")){ 
     document.getElementById("showCustResults").innerHTML=""; 
     return; 
    } 
    mlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      document.getElementById("showCustResults").innerHTML=xmlhttp.responseText; 
     } 
    } 
    var querystring = "?zip" + zip + "&st" + st ; 
    xmlhttp.open("POST","findCustomers.php" + querystring, true); 
    xmlhttp.send(); 
} 

以下是其中信息被從拉的形式:被拉動

<form id="search_customers" class="appnitro" method="post" action=""> 
     <ul> 
      <li id="li_2" > 
       <label class="description" for="zipcode">Zip Code </label> 
       <div><input id="zipcode" name="zip_code" class="element text small" type="text" maxlength="10" value=""/> </div> 
       <p class="guidelines" id="guide_2"><small>Please enter a Zip Code</small></p> 
      </li> 
      <li id="li_1" > 
       <label class="description" for="stname">Street Name </label> 
       <div><input id="stname" name="st_name" class="element text medium" type="text" maxlength="50" value=""/></div> 
       <p class="guidelines" id="guide_1"><small>Please Enter the Street Name</small></p> 
      </li> 
       <li class="buttons"> 
       <input id="findCust" class="button_text" onclick="showCustomers()" type="submit" name="find"/> 
      </li> 
     </ul> 
    </form> 
    <div id="showCustResults"><!-- Eventually search results will appear here --></div> 

而PHP鱈魚是如下:

<?php 
include 'functions.php'; #Library that holds all the functions 

#Sanitizing strings for SQL entry 
$zip = mysqli_real_escape_string($db, $_POST['zip']); 
$st = mysqli_real_escape_string($db, $_POST['st']); 

$db = db_connect(); #Connecting to the database 

#Querying the database to find any matches 
#ALSO: We might need to add another column to 
$sql = "SELECT CustomerName, ServiceAddress, BillingAddress FROM enrollment_users WHERE UserName = '$username' AND Password = '$password'"; 
$res = mysqli_query($db, $sql); 

#Creating the table to shoot out the information 
#First the header... 
echo "<table border='1'>"; 
echo " <tr>"; 
echo "  <th>Customer</th>"; 
echo "  <th>Address 1</th>"; 
echo "  <th>Address 2</th>"; 
echo "  <th>Status</th>"; 
echo " </tr>"; 

#Now the actualy information 
while($row = mysqli_fetch_assoc($res)){ 
    echo " <tr>"; 
    echo "  <td>" . $row['CustomerName'] . "</td>"; 
    echo "  <td>" . $row['ServiceAddress'] . "</td>"; 
    echo "  <td>" . $row['BillingAddress'] . "</td>"; 
    echo "  <td></td>";  
} 
echo"</table>"; 
db_close($db); #Closing the database 

?>

我一直在試圖找出過去一天無濟於事。希望有人能看到我不能。

提前感謝。

+0

如果我缺少任何信息,也請隨時告訴我。 :D – 2013-02-26 20:32:41

+0

你能解釋一下你的代碼不工作嗎? – sdespont 2013-02-26 20:34:58

+0

你不應該存儲明文密碼。用bcrypt或類似的東西對其進行哈希處理。 – Blender 2013-02-26 20:38:08

回答

2

要發送POST數據,你必須把它的發送方法而不是網址,他們必須在key=value對,你也應該encodeURIComponent編碼他們,你也必須將內容類型設置爲application/x-www-form-urlencoded

var querystring = "zip=" + encodeURIComponent(zip) + "&st=" + encodeURIComponent(phone) ; 
xmlhttp.open("POST","findCustomers.php" , true); 
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
xmlhttp.send(querystring); 
+0

從技術上講,如果他們沒有改變你的(正確)方式,他們可以從'querystring'和'$ _GET'獲取值。技術上。您的所有更改似乎都是正確的:) – Ian 2013-02-26 20:54:19

-3
mlhttp = new XMLHttpRequest(); 
xmlhttp.onreadystatechange=function() 

我沒有嘗試運行您的代碼,但乍一看它看起來像你有一個錯字,宣佈你的XMLHTTP變量。

var querystring =「?zip」+ zip +「& st」+ phone;

我也看到它看起來你的查詢字符串不正確。 AJAX代表異步JavaScript和XML(現在最常用JSON代替) 您應該在鍵和值對之間使用a:格式化查詢字符串。 http://www.json.org/

我不能確定這些將解決您的問題,因爲我沒有嘗試代碼,但我會給出幾個指針。

如果這實際上是問題,那會告訴我幾件事情。 1.你不使用你的瀏覽器開發工具,因爲如果你這樣做了,你會發現如果該變量不正確,那麼httprequest永遠不會被觸發。 2.你正在開發一個文本編輯器,而不是一個IDE。 (這是優惠我只是說這是一個觀察不是建議必然)

我不知道這項工作的目的是什麼,但即時通訊假設你不允許使用jquery,因爲$ .ajax方法將允許你可以清理這些代碼,並以更少的行數完成你想要的任務。

+0

我喜歡在執行快捷方式之前弄清楚處理事情的難題。但是在我把頭圍繞在這裏之後,我一定會檢查一下。謝謝! – 2013-02-26 22:07:30

+1

在學習的時候,首先做到這一點是可以理解和尊重的。否則,你無緣無故地撞你的頭:) – iAmClownShoe 2013-02-26 22:10:22