即使这个代码有问题,select语句只在参数为整数时返回值...查看代码..我的问题是当下拉列表的值(select)是一个整数时,select语句工作正常,否则什么也不返回。
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for 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","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
<body>
<form>
<select name="users" onchange="showUser(users.value)">
<option value="">choose a subject</option>
<option value="223">English</option>
<option value="2">ar</option>
<option value="161">علوم عامة</option>
<option value="ar">عربي</option>
</select>
<br>
<div id="txtHint"><b>Course info will be listed here.</b></div>
</p>
<p> </p>
<div class="clear"></div></div></div>
<div class="clear"></div>
</div>
</div>
</form>
</body>
//---------------and the getuser.php is
<?php
require_once("_gradeviewr.php");
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','evang_www');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"evang_www");
$sql="SELECT * FROM uploaded WHERE subject = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>subject</th>
<th>Date from</th>
<th>Date To</th>
<th>filename</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['datefrom'] . "</td>";
echo "<td>" . $row['dateto'] . "</td>";
echo "<td>" . $row['filename'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
答案 0 :(得分:0)
试试这个
HTML
<select name="users" id="urselectboxId">
脚本
$('#urselectboxId').on('change',function(){
var str=$(this).val();
if (str=="")
{
$("txtHint").text("");
return;
}
else
{
$.ajax({
url:"getuser.php",
type:"get",
dataType:'json',
data: {q:str},
success:function(data){
// codes....
}
});
}
});