我是AJAX的新手,我试图用它来获取示例用户的详细信息,一旦从用php / mysql查询生成的下拉列表中选择用户。当下拉值被硬编码(<option vlaue="1">1</option>, <option vlaue="2">2</option>, <option value="3">3</option>
等)时,该过程正常工作,但是当我使用PHP版本(下面的代码段2)时没有任何反应。我显然在这里错过了一些概念,因为我用Google搜索了每一个方式,以及通过Stackoverflow的档案搜索,但找不到答案。有很多使用AJAX来生成列表本身,但没有使用它来获取PHP变量。
非常感谢任何帮助/建议。
以下代码剪辑为:
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","tester-engine.php?q="+str,true);
xmlhttp.send();
}
表单本身(已建立数据库连接):
<form>
<select name="profile-owner" onchange="showUser(this.value)">
<?php
$contactresult = mysql_query("SELECT * FROM contacts");
while ($row = mysql_fetch_array($contactresult))
{
echo '<option value="' . $row['ContactID'] . '">' . $row['ContactLastName'] . ', ' . $row['ContactFirstName'] . '</option>';
}
</form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
最后,处理脚本(tester-engine.php):
<?php
$q=$_GET["q"];
$con = mysql_connect('####', '####', '####');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("####", $con);
$sql="SELECT * FROM contacts WHERE ContactID = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ContactFirstName'] . "</td>";
echo "<td>" . $row['ContactLastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
提前致谢!
答案 0 :(得分:0)
在评论反馈后更新了答案......
摘录2:
while ($row = mysql_fetch_array($contactresult))
{
echo '<option value="' . $row['ContactID'] . '">' . $row['ContactLastName'] . ', ' . $row['ContactFirstName'] . '</option>';
}
?>
</select>
</form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
摘录3:
<?php
$q=$_GET["q"];
// connection esablished
$sql="SELECT * FROM contacts WHERE ContactID = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ContactFirstName'] . "</td>";
echo "<td>" . $row['ContactLastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
您的最终页面需要如下所示:
<html>
<head>
<title>Hello</title>
</head>
<script type="text/javascript">
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()
{
//alert(xmlhttp.readyState + " " + xmlhttp.status);
if (xmlhttp.readyState==4 && (xmlhttp.status==200 || window.location.href.indexOf("http")==-1))
{
//alert("receieved")
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://oldkitbag.com/kerrybog/modify-profile/tester-engine.php?q="+str,true);
xmlhttp.send(null);
//alert("sent")
}
</script>
<body>
<form>
<select name="profile-owner" onchange="showUser(this.value)">
<option value="1">Griffin, Peter</option><option value="2">Griffin, Lois</option><option value="3">Quagmire, Glen</option><option value="4">Swanson, Joseph</option>
</select>
</form>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
复制并粘贴此代码进行测试,然后当您感到满意时,脚本可以使用PHP代码替换表单中的所有静态HTML文本,并且一切正常...希望!!