php - 尝试打印 MySQL 查询时出现 AJAX 问题

标签 php javascript html ajax xhtml

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

提取 cod 的 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

?>

过去一天我一直在尝试解决这个问题,但没有成功。希望有人能看到我看不到的东西。

先谢谢了。

最佳答案

要发送帖子数据,您必须将其放入发送方法而不是 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);

关于php - 尝试打印 MySQL 查询时出现 AJAX 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098704/

相关文章:

php - mysql错误: Mysql server has gone away

javascript - 如何将事件从父级传递给子级以与功能组件发生 react ?

javascript - 使用 Google map MarkerCluster for api v3 时如何围绕制造商创建圆圈

javascript - 如何仅在 CSS 中创建具有绝对顶部和底部以及固定边的边框?包括图像

html - 如何在odoo报告上添加静态字段而不循环?

php - 根据数据库中的表检查 url

php - 检索访问者的浏览器/版本并将其存储在数据库中

javascript - 如何在 jquery-ui 中生成事件生成

html - Flexbox 网格 - 对齐 : space-around and aligning last items left

php - 简单的HTML DOM-(直接)子选择器