简体   繁体   English

下拉列表未填充Ajax

[英]dropdown list not populating with Ajax

Whilst I'm sure it must be something really obvious, I can't see where I am going wrong with this. 虽然我敢肯定这一定是很明显的,但是我看不出我在哪里出错了。 I have a drop down list with two options in it. 我有一个下拉列表,其中有两个选项。 When I Select an option it should use XMLHttpRequest() to get a list of customers from the database, based on the option selected. 当我选择一个选项时,它应根据选择的选项使用XMLHttpRequest()从数据库中获取客户列表。

I have two parts: 我分为两个部分:

ajax2_js.php - contains the javascript and html form. ajax2_js.php-包含javascript和html表单。

ajax2_DBAccess.php - contains the PHP that gets the list from the databse. ajax2_DBAccess.php-包含从数据库获取列表的PHP。

I have checked everything on the second page, and this works fine on it's own (and displays the relevant list as a dropdown menu), but when I select the option on the first page, nothing happens. 我已经检查了第二页上的所有内容,并且可以单独运行(并且将相关列表显示为下拉菜单),但是当我在第一页上选择该选项时,没有任何反应。

My code thus far is: 到目前为止,我的代码是:

ajax2_js.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 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();
            }   
    };
?>

Any assistance would be appreciated. 任何援助将不胜感激。

Check the javascript error log. 检查javascript错误日志。 This might be the problem, a spelling error in "Request". 这可能是问题所在,是“请求”中的拼写错误。 ajaxResuest.open("GET", strURL, true); ajaxResuest.open( “GET”,strURL,真正的);

Also, your SQL query suffers from an SQL injection vulnerability in the $network parameter. 另外,您的SQL查询在$ network参数中还存在SQL注入漏洞。

You can either use XML or JSON to return list. 您可以使用XML或JSON返回列表。 This tutorial should help. 教程应该会有所帮助。 I personally would use XML. 我个人将使用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();

but there are plenty of tutorials on both methods. 但是有很多关于这两种方法的教程。

Thanks for all your help guys, I have tracked it down to three things (all my fault): 感谢您的帮助,我将其归结为三件事(都是我的错):

function ajaxFunction()

should be: 应该:

function ajaxFunction(strURL)

.

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

should be: 应该:

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

.

and finally: 最后:

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

should be 应该

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

.

(and of course the SQL injection vulnerability mentioned above which I will also fix). (当然还有上面提到的SQL注入漏洞,我也会修复)。

cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM