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

Php 尝试打印MySQL查询时出现AJAX问题,php,javascript,html,ajax,xhtml,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==""

我目前正在学习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();
}

函数showCustomers()
{
var zip=document.getElementById('zipcode').value;
var st=document.getElementById('stname')。值;
如果((zip==“”)和(&(st==“”)){
document.getElementById(“showCustResults”).innerHTML=“”;
回来
} 
mlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“showCustResults”).innerHTML=xmlhttp.responseText;
}
}
var querystring=“?zip”+zip+“&st”+st;
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

在过去的一天里,我一直在试图弄明白这一点,但毫无结果。希望有人能看到我看不到的东西


谢谢

要发送post数据,您必须将其放入send方法而不是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);
我没有尝试运行您的代码,但乍一看,您似乎有一个输入错误,声明了xmlhttp变量

var querystring=“?zip”+zip+“&st”+电话

我还发现您的查询字符串似乎不正确。AJAX代表异步javascript和xml(现在最常用的是JSON) 您应该在键和值对之间使用:设置查询字符串的格式。

我不能肯定这些会解决你的问题,因为我还没有尝试过代码,但我会给几个指针

如果这真的是问题所在,那就告诉我一些事情。 1.您不使用浏览器开发人员工具,因为如果您这样做,您将看到如果该变量不正确,httprequest永远不会被触发。 2.您是在文本编辑器而不是IDE中开发的。(这是优先考虑的,我只是说这是一个观察,不一定是一个建议)


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

如果我遗漏了任何信息,请随时通知我:你能解释一下你的代码哪部分不起作用吗?你不应该存储明文密码。用bcrypt或类似的方法对它们进行散列。从技术上讲,如果他们没有将其更改为您的(正确)方式,他们可以使用
$\u get
查询字符串中获取值。从技术上讲你所有的改变似乎都是对的:)我喜欢在做捷径之前先弄清楚做事情的困难之处。但我一定会在脑子里想清楚这件事之后再去看看。谢谢在学习的时候,先用艰苦的方式去做是可以理解的,也是值得尊敬的。否则你会无缘无故地撞到你的头:)
mlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()