- PHP教程
 - PHP-主页
 - PHP - 简介
 - PHP-环境设置
 - PHP - 语法概述
 - PHP - 变量类型
 - PHP - 常量
 - PHP - 运算符类型
 - PHP - 决策
 - PHP - 循环类型
 - PHP-数组
 - PHP-字符串
 - PHP - 网络概念
 - PHP - 获取和发布
 - PHP - 文件包含
 - PHP - 文件和 I/O
 - PHP - 函数
 - PHP-Cookie
 - PHP-会话
 - PHP - 发送电子邮件
 - PHP-文件上传
 - PHP - 编码标准
 
- 高级PHP
 - PHP - 预定义变量
 - PHP-正则表达式
 - PHP - 错误处理
 - PHP - 错误调试
 - PHP - 日期和时间
 - PHP 和 MySQL
 - PHP 和 AJAX
 - PHP 和 XML
 - PHP——面向对象
 - PHP - 面向 C 开发人员
 - PHP - 适合 PERL 开发人员
 
- PHP 表单示例
 - PHP-表单介绍
 - PHP - 验证示例
 - PHP - 完整表格
 
- PHP框架作品
 - PHP-框架工程
 - PHP - 核心 PHP 与 Frame Works
 
- PHP 设计模式
 - PHP - 设计模式
 
- PHP 函数参考
 - PHP - 内置函数
 
- PHP 有用资源
 - PHP - 问题与解答
 - PHP - 有用的资源
 - PHP - 讨论
 
PHP - Ajax 自动完成搜索
自动完成搜索
当您在字段中输入数据时,自动完成搜索框会提供建议。这里我们使用xml来调用自动完成建议。下面的示例演示了如何使用 php 的自动完成文本框。
索引页
索引页应如下所示 -
<html>
   <head>
      
      <style>
         div {
            width:240px;
            color:green;
         }
      </style>
      
      <script>
         function showResult(str) {
			
            if (str.length == 0) {
               document.getElementById("livesearch").innerHTML = "";
               document.getElementById("livesearch").style.border = "0px";
               return;
            }
            
            if (window.XMLHttpRequest) {
               xmlhttp = new XMLHttpRequest();
            }else {
               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>
         <h2>Enter Course Name</h2>
         <input type = "text" size = "30" onkeyup = "showResult(this.value)">
         <div id = "livesearch"></div>
         <a href = "http://www.tutorialspoint.com">More Details </a>
      </form>
      
   </body>
</html>
实时搜索.php
它用于从 xml 文件调用数据,并将结果发送到 Web 浏览器。
<?php
   $xmlDoc = new DOMDocument();
   $xmlDoc->load("autocomplete.xml");
   $x = $xmlDoc->getElementsByTagName('link');
   $q = $_GET["q"];
   
   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) {
            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>";
               }
            }
         }
      }
   }
	
   if ($hint == "") {
      $response = "Please enter a valid name";
   }else {
      $response = $hint;
   }
   echo $response;
?>
自动完成.xml
它包含自动完成数据,并由 livesearch.php 根据标题字段和 URL 字段进行访问
<pages>
   <link>
      <title>android</title>
      <url>http://www.tutorialspoint.com/android/index.htm</url>
   </link>
   <link>
      <title>Java</title>
      <url>http://www.tutorialspoint.com/java/index.htm</url>
   </link>
   <link>
      <title>CSS </title>
      <url>http://www.tutorialspoint.com/css/index.htm</url>
   </link>
   <link>
      <title>angularjs</title>
      <url>http://www.tutorialspoint.com/angularjs/index.htm </url>
   </link>
   <link>
      <title>hadoop</title>
      <url>http://www.tutorialspoint.com/hadoop/index.htm </url>
   </link>
   <link>
      <title>swift</title>
      <url>http://www.tutorialspoint.com/swift/index.htm </url>
   </link>
   <link>
      <title>ruby</title>
      <url>http://www.tutorialspoint.com/ruby/index.htm </url>
   </link>
   <link>
      <title>nodejs</title>
      <url>http://www.tutorialspoint.com/nodejs/index.htm </url>
   </link>
</pages>
它将产生以下结果 -

