我正在使用对PHP文件的Ajax调用来从MySQL数据库获取数据,并在HTML中填充选择选项。问题是选项中的重复项目,我不知道为什么。我在工作台中试过了这个查询,它带回了我需要的东西。为什么我从MySQL查询中获取重复的项目?
PHP文件:
<?php
$q=$_GET["q"];
// open db connection code
$query = "select * from r2rtool.materialtype where type = 'FE' and tools like '%".$q."%'";
$result = mysql_query($query);
$option = "";
while($row = mysql_fetch_array($result))
{
$mat = $row["Material"];
$option.="<option value=\"$mat\">".$mat."</option>";
echo $option;
}
// close db connection
?>
的Ajax功能:
function populatematerial(str)
{
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","phpfile.php?q="+str,true);
xmlhttp.send();
}
换句话说,在完成检索/构建数据后执行输出。您的版本正在建设,输出,增加一点,再次输出等... –
它不完全重复,你得到像1 12 123 1234 12345 ... –
我爱你。工作完美。 :-D – TheRealDK