[php学习六]AJAX练习3-实时搜索

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实时搜索</title>

    <script language="JavaScript">
        function onSearch(SearText){
            resultDIV = document.getElementById("ResultDIV");
            if (SearText.length == 0){
                resultDIV.innerHTML = "";
                resultDIV.style.border = "0px";
                return;
            }
            xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.status ==200 && xmlhttp.readyState == 4){
                    resultDIV.innerHTML = xmlhttp.responseText;
                    resultDIV.style.border = "1px";
                }
            };
            xmlhttp.open("GET","实时搜索.php?SearchText=" + SearText, true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
<h3>请输入要搜索内容:</h3>
<!--这个Input没有Title还提示错误~~-->
<input title="搜索框" type="text" style="width: 200px" id="SearchText" οnkeyup="onSearch(this.value)">
<div id="ResultDIV"></div>
</body>
</html>
<html>
<head>
    <meta charset="utf-8">
</head>
</html>
<?php
/**
 * Created by PhpStorm.
 * User: liyanq
 * Date: 16/7/18
 * Time: 19:49
 */
$SerchText = $_GET["SearchText"];
$xmlDOC = new DOMDocument();
$xmlDOC->load("links.xml");
$x = $xmlDOC->getElementsByTagName("link");
$result = "";
for ($i = 0; $i < ($x->length); $i ++){
    /*这里试验了半天,为神马索引不是0和1呢????*/
    $y=$x->item($i)->childNodes->item(1);
    $z=$x->item($i)->childNodes->item(3);
    if(($y->nodeType == 1) && ($z->nodeType == 1)){
        /*stristr这个函数还没查怎么用~~*/
        if(stristr($y->nodeValue, $SerchText) || stristr($z->nodeValue, $SerchText)){
            if (empty($result)){
                $result = "<a href ='http://www.baidu.com' target='_blank'> ".
                          $z->nodeValue.$y->nodeValue."</a><br>";
            }else
            {
                $result =$result . "<a href ='http://www.baidu.com' target='_blank'>".
                         $z->nodeValue.$y->nodeValue."</a><br>";
            }
        }
    }
}
if (empty($result)){
    echo "没有找到内容".$SerchText;
}else{
    echo $result;
}
?>

xml 文件内容

<?xml version="1.0" encoding="UTF-8"?>

<!-- Edited by XMLSpy® -->

<pages>

<link>

<title>HTML a 标签</title>

<url>http://www.runoob.com/tags/tag-a.html</url>

</link>

<link>

<title>HTML br 标签</title>

<url>http://www.runoob.com/tags/tag-br.html</url>

</link>

<link>

<title>CSS background 属性</title>

<url>http://www.runoob.com/cssref/css3-pr-background.html</url>

</link>

<link>

<title>CSS border 属性</title>

<url>http://www.runoob.com/cssref/pr-border.html</url>

</link>

<link>

<title>JavaScript Date 对象</title>

<url>http://www.runoob.com/jsref/jsref-obj-date.html</url>

</link>

<link>

<title>JavaScript Array 对象</title>

<url>http://www.runoob.com/jsref/jsref-obj-array.html</url>

</link>

</pages>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值