2013-02-26 81 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