我在创建使用PHP和XML的网页时相对较新,但我对在W3S学校看到的somthine很感兴趣。我想创建一个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;
?>
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>
感谢您的帮助
答案 0 :(得分:1)
我将其复制到文本管理器中并保存在文档文件夹中的文件夹中。这些都不在网络服务器中,因为我认为我不需要它,但是当它没有用时却很惊讶。
PHP脚本由安装了PHP引擎的Web服务器执行。要正确执行livescript.php,请先在您的计算机上安装Web服务器软件,或从托管服务提供商处租用托管空间。
获得Web服务器后,将文件安装在Web服务器引用的目录中(通常在基于Unix的服务器上/home/<username>/public_html
),并通过http://yourdomain.com/index.html访问HTML脚本。
答案 1 :(得分:0)
您似乎使用简单的javascript发送ajax请求,jQuery
更容易。您不必检查浏览器,jQuery中简化了许多其他内容。
现在为流程部分。
1.必须发生触发ajax请求的事件。它可以是你选择的模糊,聚焦,点击,加载,鼠标移除,鼠标等任何东西。 代码可能看起来像这样;
$($btn).click(function(){
insert your ajax request here
})
这表示该按钮已被点击
2.call 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请求的回复只是hello world
。
4.处理ajax success function
success : function (response){ //the variable in the function can be anything
alert(response);
}
以上示例将alert 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>
这是php文件 phpFile.php
echo "Hello world";