1

我目前正在学习 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

?>

过去一天我一直试图弄清楚这一点,但无济于事。希望有人能看到我看不到的东西。

提前谢谢。

4

2 回答 2

2

要发送帖子数据,您必须将其放入发送方法而不是 url,它们必须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);
于 2013-02-26T20:38:39.427 回答
-3
mlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()

我没有尝试运行您的代码,但乍一看,您似乎有一个错字声明您的 xmlhttp 变量。

var querystring = "?zip" + zip + "&st" + phone ;

我还看到您的查询字符串似乎不正确。AJAX 代表异步 javascript 和 xml(现在最常替换为 JSON),您应该在键值对之间使用 : 格式化查询字符串。 http://www.json.org/

我不确定这些是否会解决您的问题,因为我没有尝试过代码,但我会给出一些建议。

如果这确实是问题所在,那将告诉我一些事情。1. 你不要使用你的浏览器开发者工具,因为如果你这样做了,你会看到如果那个变量不正确,一个 httprequest 永远不会被触发。2. 你是在文本编辑器而不是 IDE 中开发的。(这是优先的,我只是将其作为观察而不是推荐)

我不知道这项工作的目的是什么,但我假设你不允许使用 jquery,因为 $.ajax 方法将允许你清理这段代码并在更少的行中完成你想要的。

于 2013-02-26T20:45:23.507 回答