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 实时搜索
AJAX Live Search
在下面的实例中,我们将演示一个实时的搜索,在您键入数据的同时即可得到搜索结果。
实时的搜索与传统的搜索相比,具有很多优势:
- 当键入数据时,就会显示出匹配的结果
- 当继续键入数据时,对结果进行过滤
- 如果结果太少,删除字符就可以获得更宽的范围
在下面的文本框中搜索 test 的页面
上面实例中的结果在一个 XML 文件(links.xml)中进行查找。为了让这个例子小而简单,我们只提供 6 个结果。
实例解释 - HTML 页面
当用户在上面的输入框中键入字符时,会执行 "showResult()" 函数。该函数由 "onkeyup" 事件触发:
<html>
<head>
<script>
function showResult(str)
{
if (str.length==0)
{
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
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("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</body>
</html>
源代码解释:
如果输入框是空的(str.length==0),该函数会清空 livesearch 占位符的内容,并退出该函数。
如果输入框不是空的,那么 showResult() 会执行以下步骤:
- 创建 XMLHttpRequest 对象
- 创建在服务器响应就绪时执行的函数
- 向服务器上的文件发送请求
- 请注意添加到 URL 末端的参数(q)(包含输入框的内容)
PHP 文件
上面这段通过 JavaScript 调用的服务器页面是名为 "livesearch.php" 的 PHP 文件。
"livesearch.php" 中的源代码会搜索 XML 文件中匹配搜索字符串的标题,并返回结果:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1)
{
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
else
{
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
如果 JavaScript 发送了任何文本(即 strlen($q) > 0),则会发生:
- 加载 XML 文件到新的 XML DOM 对象
- 遍历所有的 <title> 元素,以便找到匹配 JavaScript 所传文本
- 在 "$response" 变量中设置正确的 URL 和标题。如果找到多于一个匹配,所有的匹配都会添加到变量。
- 如果没有找到匹配,则把 $response 变量设置为 "no suggestion"。