我目前正在学习 AJAX,我遇到了这个错误,其中没有显示 MySQL 查询的结果。
以下片段是 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
?>
过去一天我一直试图弄清楚这一点,但无济于事。希望有人能看到我看不到的东西。
提前谢谢。