Ajax传递下拉菜单值


Ajax pass through dropdown menu value

我试图通过所选下拉菜单项的值,使用下面的代码,我可以使用单个数字(如"1"或"2")来完成此操作,但当我尝试使用文本时,值被设置为"0"。任何Idea的值都需要使用Ajax在另一个页面上运行变量。

HTML页面======

 <html>
<head>
<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>
</head>
<body>
<form>
<select name="users" onChange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="B0012345">B0012345</option>
<option value="COM601">COM601</option>
<option value="3">ID Nothing</option>
<option value="4">ID Four</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>  

PHP页面=======

<?php include "db.php" ?>
<?php
$q = intval($_get['q']);
echo $q;

$result = mysqli_query($db_connection, "SELECT * FROM feedback WHERE ModuleCode = '".$q."'");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['StudentID'] . "</td>";
  echo "<td>" . $row['FeedbackID'] . "</td>";
  echo "<td>" . $row['ModuleCode'] . "</td>";
  echo "<td>" . $row['Viewed'] . "</td>";
  echo "<td>" . $row['StudentComment'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

echo "Result kk" . $row;
mysqli_close($con);
?> 

你没有收到$_get varibale的通知吗?如果没有,则必须启用错误报告。由于$_get['q']未定义,因此需要将其更改为$_GET['q']$_GET是PHP中的一个超级全局变量,用于访问通过查询字符串传递的值。有关更多信息,您可以访问php.net网站