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 ではなく send メソッドに入れる必要があります。それらは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 ではなく、テキスト エディターで開発している。(これは優先事項です。必ずしも推奨ではなく、観察としてこれを言っているだけです)

この作業の目的はわかりませんが、$.ajax メソッドを使用すると、このコードをかなりクリーンアップして、より少ない行で目的を達成できるため、jquery の使用が許可されていないと仮定します。

于 2013-02-26T20:45:23.507 に答える