PHP 教程
- 第1节:PHP 简介
- 第2节:PHP 安装
- 第3节:PHP 语法
- 第4节:PHP 变量
- 第5节:PHP echo和print 语句
- 第6节:PHP 数据类型
- 第7节:PHP 常量
- 第8节:PHP 字符串变量
- 第9节:PHP 运算符
- 第10节:PHP if...else 语句
- 第11节:PHP switch 语句
- 第12节:PHP 数组
- 第13节:PHP 数组排序
- 第14节:PHP 超级全局变量
- 第15节:PHP while 循环
- 第16节:PHP for 循环
- 第17节:PHP 函数
- 第18节:PHP 魔术常量
- 第19节:PHP 命名空间
- 第20节:PHP 面向对象
- 第21节:PHP 表单
- 第22节:PHP 表单验证
- 第23节:PHP 表单 - 必需字段
- 第24节:PHP 表单 - 验证邮件和URL
- 第25节:PHP 完整表单实例
- 第26节:PHP $_GET 变量
- 第27节:PHP $_POST 变量
- 第28节:PHP 多维数组
- 第29节:PHP 日期
- 第30节:PHP date() 函数
- 第31节:PHP 包含
- 第32节:PHP 文件
- 第33节:PHP 文件上传
- 第34节:PHP Cookies
- 第35节:PHP Sessions
- 第36节:PHP E-Mail
- 第37节:PHP 安全 E-mail
- 第38节:PHP 错误处理
- 第39节:PHP 异常处理
- 第40节:PHP 过滤器
- 第41节:PHP 高级过滤器
- 第42节:PHP JSON
- 第43节:PHP MySQL 简介
- 第44节:PHP 连接 MySQL
- 第45节:PHP MySQL 创建数据库
- 第46节:PHP MySQL 创建数据表
- 第47节:PHP MySQL 插入数据
- 第48节:PHP MySQL 插入多条数据
- 第49节:PHP MySQL 预处理语句
- 第50节:PHP MySQL 读取数据
- 第51节:PHP MySQL Where 子句
- 第52节:PHP MySQL Order By 关键词
- 第53节:PHP MySQL Update
- 第54节:PHP MySQL Delete From
- 第55节:PHP 数据库 ODBC
- 第56节:PHP XML Expat 解析器
- 第57节:PHP XML DOM
- 第58节:PHP XML SimpleXML
- 第59节:PHP AJAX 简介
- 第60节:PHP – AJAX 与 PHP
- 第61节:PHP 实例 AJAX 与 MySQL
- 第62节:PHP 实例 AJAX 与 XML
- 第63节:PHP 实例 AJAX 实时搜索
- 第64节:PHP 实例 AJAX RSS 阅读器
- 第65节:PHP 实例 AJAX 投票
- 第66节:PHP 5 Array 函数
- 第67节:PHP 5 Calendar 函数
- 第68节:PHP cURL 函数
- 第69节:PHP 5 Date/Time 函数
- 第70节:PHP 5 Directory 函数
- 第71节:PHP Error 和 Logging 函数
- 第72节:PHP 5 Filesystem 函数
- 第73节:PHP Filter 函数
- 第74节:PHP FTP 函数
- 第75节:PHP HTTP 函数
- 第76节:PHP Libxml 函数
- 第77节:PHP Mail 函数
- 第78节:PHP 5 Math 函数
- 第79节:PHP Misc 函数
- 第80节:PHP MySQLi 函数
- 第81节:PHP PDO
- 第82节:PHP SimpleXML 函数
- 第83节:PHP String 函数
- 第84节:PHP XML 函数
- 第85节:PHP Zip File 函数
- 第86节:PHP Timezones
- 第87节:PHP 图像处理
- 第88节:PHP 序列化/反序列化函数
- 第89节:PHP 常用header头定义
PHP 实例 AJAX 与 MySQL
AJAX 数据库实例
下面的实例将演示网页如何通过 AJAX 从数据库读取信息:
实例
Person info will be listed here...
实例解释 - MySQL 数据库
在上面的实例中,我们使用的数据库表如下所示:
实例解释 - HTML 页面
当用户在上面的下拉列表中选择某位用户时,会执行名为 "showUser()" 的函数。该函数由 "onchange" 事件触发:
<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="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
showUser() 函数会执行以下步骤:
- 检查是否有用户被选择
- 创建 XMLHttpRequest 对象
- 创建在服务器响应就绪时执行的函数
- 向服务器上的文件发送请求
- 请注意添加到 URL 末端的参数(q)(包含下拉列表的内容)
PHP 文件
上面这段通过 JavaScript 调用的服务器页面是名为 "getuser.php" 的 PHP 文件。
"getuser.php" 中的源代码会运行一次针对 MySQL 数据库的查询,然后在 HTML 表格中返回结果:
<?php
$q=$_GET["q"];
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
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['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>";
mysqli_close($con);
?>
解释:当查询从 JavaScript 发送到 PHP 文件时,将发生:
- PHP 打开一个到 MySQL 数据库的连接
- 找到选中的用户
- 创建 HTML 表格,填充数据,并发送回 "txtHint" 占位符