簡體   English   中英

下拉列表未填充Ajax

[英]dropdown list not populating with Ajax

雖然我敢肯定這一定是很明顯的,但是我看不出我在哪里出錯了。 我有一個下拉列表,其中有兩個選項。 當我選擇一個選項時,它應根據選擇的選項使用XMLHttpRequest()從數據庫中獲取客戶列表。

我分為兩個部分:

ajax2_js.php-包含javascript和html表單。

ajax2_DBAccess.php-包含從數據庫獲取列表的PHP。

我已經檢查了第二頁上的所有內容,並且可以單獨運行(並且將相關列表顯示為下拉菜單),但是當我在第一頁上選擇該選項時,沒有任何反應。

到目前為止,我的代碼是:

ajax2_js.php

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <script>
function ajaxFunction()
{
var ajaxRequest;
ajaxRequest = new XMLHttpRequest();

ajaxRequest.onreadystatechange = function()
    {
        if(ajaxRequest.readyState == 4)
            {
                document.getElementById('customerDiv').innerHTML=req.responseText;
            }
    }
ajaxResuest.open("GET", strURL, true);
ajaxRequest.send(null); 
}
        </script>           
</head>

<body>
    <form method="post" action="" name="form1">
        Network : <select name="network" onChange="ajaxFunction('ajax2_DBAccess.php?network='+this.value)">
            <option value="">Select Network</option>
            <option value="1">Net1</option>
            <option value="2">Net2</option>
         </select>
        </br>
        Customer : <div id="customerDiv">
            <select name="select">
                <option>Select Customer</option>
            </select>
        </div>
    </form>
</body>

ajax2_DBAccess.php

<?php
$network=$_GET['network'];
$q1 = "SELECT `CustName` FROM monitor.customers where network = $network;";

$con = new mysqli('localhost:3306', 'xxx', 'xxx');

if (mysqli_connect_errno()) 
    {
        $error = mysqli_connect_error();
        echo $error;
        exit();
    }
else
    {   
        $ConfRes = mysqli_query($con, $q1); 
        if ($ConfRes)
            {
                echo "<select name=\"Customers\">";
                echo "<option>Select Customer</option>";
                while($row=mysqli_fetch_array($ConfRes, MYSQLI_ASSOC))
                    { 
                        $result = $row['CustName'];
                        echo "<option value>$result</option>";
                    };
                echo "</select>";
            }
        else
            {   
                $error = mysqli_error();
                echo $error;
                exit();
            }   
    };
?>

任何援助將不勝感激。

檢查javascript錯誤日志。 這可能是問題所在,是“請求”中的拼寫錯誤。 ajaxResuest.open( “GET”,strURL,真正的);

另外,您的SQL查詢在$ network參數中還存在SQL注入漏洞。

您可以使用XML或JSON返回列表。 教程應該會有所幫助。 我個人將使用XML。

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   
  $newnode->setAttribute("CustName",$row['CustName']);
  } 

echo $dom->saveXML();

但是有很多關於這兩種方法的教程。

感謝您的幫助,我將其歸結為三件事(都是我的錯):

function ajaxFunction()

應該:

function ajaxFunction(strURL)

ajaxResuest.open("GET", strURL, true);

應該:

ajaxRequest.open("GET", strURL, true);

最后:

document.getElementById('customerDiv').innerHTML=req.responseText;

應該

document.getElementById('customerDiv').innerHTML=ajaxRequest.responseText;

(當然還有上面提到的SQL注入漏洞,我也會修復)。

干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM