[英]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.