PHP 示例 AJAX 实时搜索

标签 php html mysql ajax livesearch

对于创建使用 PHP 和 XML 的网页,我还比较陌生,但我对在 W3S 学校看到的一些东西很感兴趣。我想创建一个 AJAX 实时搜索到示例页面中显示的搜索,但首先我需要帮助学习如何运行它。(http://www.w3schools.com/php/php_ajax_livesearch.asp)我复制粘贴了网站中的三个代码文件,当我单击 html 文件,我得到的只是一个空表单框。我需要以某种方式将其与 MySql 链接吗?如果是这样我该怎么做?

index.html:

<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> 

livesearch.php:

<?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;
?> 

链接.xml:

<!-- Edited by XMLSpy® --><pages><link><title>HTML a tag</title><url>http://www.w3schools.com/tags/tag_a.asp</url></link><link><title>HTML br tag</title><url>http://www.w3schools.com/tags/tag_br.asp</url></link><link><title>CSS background Property</title><url>http://www.w3schools.com/cssref/css3_pr_background.asp</url></link><link><title>CSS border Property</title><url>http://www.w3schools.com/cssref/pr_border.asp</url></link><link><title>JavaScript Date Object</title><url>http://www.w3schools.com/jsref/jsref_obj_date.asp</url></link><link><title>JavaScript Array Object</title><url>http://www.w3schools.com/jsref/jsref_obj_array.asp</url></link></pages>

感谢您的帮助

最佳答案

i copied this into text wrangler and saved in a folder in documents folder. None of this is in a webserver as i didnt think i would need it but was suprised when it didnt work.

PHP 脚本由安装了 PHP 引擎的 Web 服务器执行。要正确执行 livescript.php,请首先在您的计算机上安装 Web 服务器软件或从托管提供商处租用托管空间。

当您获得 Web 服务器后,将文件安装在 Web 服务器引用的目录中(在基于 Unix 的服务器上通常为 /home/<username>/public_html)并通过以下方式访问您的 HTML 脚本: http://yourdomain.com/index.html

关于PHP 示例 AJAX 实时搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17193409/

相关文章:

html - 为什么我的表格没有显示边框?

php - 如何使用 php 链接电子邮件地址

php - 针对内部页面元素的 CSS

php - SQL查询消息投票

php - 套接字绑定(bind)() : unable to bind address [99] (Ubuntu on Amazon EC2)

php - 在php中为url编码中文字符

mysql - 最佳数据库,NOSQL 与 MySQL

php - GLOBALS 参数的使用

mysql - 'column-adding'(模式修改)是 NoSQL(mongodb)数据库相对于 MySQL 等 RDBMS 的关键优势吗

javascript - 如何使用查询将 UNIX 时间戳插入到列中?