该页面用 PHP 编写,并使用 MySQL 数据库。
其中的代码执行针对数据库的 SQL 查询,并以 HTML 表格返回结果:
<?php
$q=$_GET["q"]
$con = mysql_connect('localhost', 'peter', 'abc123')
if (!$con)
{
die('Could not connect: ' . mysql_error())
}
mysql_select_db("ajax_demo", $con)
$sql="SELECT * FROM user WHERE id = '".$q."'"
$result = mysql_query($sql)
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>"
while($row = mysql_fetch_array($result))
{
echo "<tr>"
echo "<td>" . $row['FirstName'] . "</td>"
echo "<td>" . $row['LastName'] . "</td>"
echo "<td>" . $row['Age'] . "</td>"
echo "<td>" . $row['Hometown'] . "</td>"
echo "<td>" . $row['Job'] . "</td>"
echo "</tr>"
}
echo "</table>"
mysql_close($con)
?>
例子解释:
当查询从 JavaScript 被发送到这个 PHP 页面,会发生:
PHP 打开到达 MySQL 服务器的连接
找到拥有指定姓名的 "user"
创建表格,插入数据,然后将其发送到 "txtHint" 占位符
function updatePage(targetObject){if(xmlHttp.readyState == 4){
var response = xmlHttp.responseText//把AJAX返回的值赋给response.如果你想使用这个变量,可以直接调用,也可以return到函数外调用.
document.getElementById(targetObject).innerHTML = response//把response 的值插入html的DIV中,
}
}
上面是一个简单的获取php文件处理后的函数,请看response ,其实他就是你所需要的效果.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)