PHP示例AJAX实时搜索

PHP示例AJAX实时搜索,php,html,mysql,ajax,livesearch,Php,Html,Mysql,Ajax,Livesearch,在创建使用PHP和XML的网页方面,我是一个比较新的人,但我对我在W3S学校看到的Somthy感兴趣。我想创建一个AJAX live搜索,它显示在示例页面中,但首先我需要帮助学习如何运行。()我复制粘贴了网站中的三个代码文件,当我单击html文件时,我得到的只是一个空表单框。我是否需要以某种方式将其与MySql链接?如果是这样的话,我该怎么做呢 index.html: <html> <head> <script> function showResult(str

在创建使用PHP和XML的网页方面,我是一个比较新的人,但我对我在W3S学校看到的Somthy感兴趣。我想创建一个AJAX live搜索,它显示在示例页面中,但首先我需要帮助学习如何运行。()我复制粘贴了网站中的三个代码文件,当我单击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> 

函数显示结果(str)
{
如果(str.length==0)
{
document.getElementById(“livesearch”).innerHTML=“”;
document.getElementById(“livesearch”).style.border=“0px”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“livesearch”).innerHTML=xmlhttp.responseText;
document.getElementById(“livesearch”).style.border=“1px solid#A5ACB2”;
}
}
open(“GET”,“livesearch.php?q=“+str,true”);
xmlhttp.send();
}
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;
?> 
load(“links.xml”);
$x=$xmlDoc->getElementsByTagName('link');
//从URL获取q参数
$q=$_GET[“q”];
//如果q的长度>0,则从xml文件中查找所有链接
如果(斯特伦($q)>0)
{
$hint=“”;
对于($i=0;$i长度);$i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
如果($y->item(0)->nodeType==1)
{
//查找与搜索文本匹配的链接
if(stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
如果($hint==“”)
{
$hint=“”;
}
其他的
{
$hint=$hint.“
”; } } } } } //如果未找到提示,则将输出设置为“无建议” //或设置为正确的值 如果($hint==“”) { $response=“无建议”; } 其他的 { $response=$hint; } //输出响应 回音$应答; ?>
links.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>
HTMLtaghttp://www.w3schools.com/tags/tag_a.aspHTML 溴taghttp://www.w3schools.com/tags/tag_br.aspCSS 背景专业pertyhttp://www.w3schools.com/cssref/css3_pr_background.aspCSS 边境专业人士pertyhttp://www.w3schools.com/cssref/pr_border.aspJavaScript 日期Objecthttp://www.w3schools.com/jsref/jsref_obj_date.aspJavaScript 排列Objecthttp://www.w3schools.com/jsref/jsref_obj_array.asp

感谢您的帮助

您似乎正在使用简单的javascript发送ajax请求,使用
jQuery
更容易。您不必检查浏览器,jQuery简化了许多其他内容。 现在是流程部分

1.必须发生一个事件来触发ajax请求。它可以是模糊、聚焦、点击、加载、鼠标输出、鼠标输入等任何形式,这是您的选择。 代码可能是这样的

$($btn).click(function(){

insert your ajax request here

})
这表示该按钮已被单击

2.调用ajax

   $.ajax({

    url : "phpFile.php",

    data : dataYouwantToSend,

    success: function() {

    code to do if the call is successful
    }

    })
3.处理php文件中的数据

phpFile.php中

php文件中的任何回显或打印内容都将显示为文件的响应

比如说 如果您的php文件仅包含

echo "hello world";
对ajax请求的响应将是
helloworld

4.在
ajax成功函数中处理响应

success : function (response){ //the variable in the function can be anything
alert(response);
}
上述示例将
提醒hello world

整个代码如下所示。这是html文件

<input type="text" id="clickMe" />
<script src="ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function(){
$("#clickMe").click(function(){
$.ajax({
url : "phpFile.php",
success : function(res) {
alert(res);
}
})
})
})
</script>
我将其复制到文本牧马人中,并保存在文档文件夹中的一个文件夹中。这些都不在Web服务器中,因为我认为我不需要它,但当它不工作时,我感到惊讶

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


获得web服务器后,将文件安装到web服务器引用的目录中(在基于Unix的服务器上通常是
/home//public\u html
),并通过以下方式访问html脚本:

您可能需要查看所选的主搜索。真是太棒了!您是否已将这些文件复制到Web服务器?对于本例,您不需要访问数据库。所有相关数据都在.xml文件@sofl中,我将其复制到了text wrangler中,并保存在documents文件夹中的一个文件夹中。这些都不在Web服务器中,因为我认为我不需要它,但当它不工作时,我感到惊讶。它们只是同一文件夹中的三个独立文件,与我复制粘贴的方式相同@SiKni8你能放置一个链接吗,谷歌只是给出了传说联盟的东西在这种情况下,这个例子无论如何都不能工作。您需要一个用于PHP和xhr的Web服务器。你到底想要什么?如果没有Web服务器,您可以使用静态或远程列表(jsonp请求)创建自动完成。我建议使用jqueryUI来实现这一点。。看看这些例子:只是一个提醒,是的,谢谢,我没有意识到这会是一个问题。我现在还不太喜欢尝试使用这个服务器,但是当我把它放进public_html文件夹并在线检查时,搜索框就工作了。
echo "Hello world";