PHP example - AJAX real-time search

PHP Example - AJAX Real-time Search

AJAX can provide users with a more friendly and interactive search experience.

AJAX Live Search

In the following example, we will demonstrate a real-time search, and you can get the search results while you type the data.

Real-time search has many advantages over traditional search:

When you type in data, matching results will be displayed

When you continue to type in data, Filter the results

If there are too few results, delete characters to get a wider range

Enter "HTML" in the text box below to search for pages containing HTML:

QQ图片20161010103643.png

The results in the above example are searched in an XML file (links.xml). To keep this example small and simple, we only provide 6 results.

Explanation of examples - HTML page

When the user types characters in the input box above, the "showResult()" function will be executed. This function is triggered by the "onkeyup" event:

   

Source code explanation:

If the input box is empty (str.length==0), this function will clear the livesearch placeholder content and exit the function.

If the input box is not empty, then showResult() will perform the following steps:

Create an XMLHttpRequest object

Create a function that is executed when the server response is ready

Send a request to the file on the server

Please pay attention to the parameter (q) added to the end of the URL (contains the content of the input box)

PHP file

The above paragraph The server page called via JavaScript is a PHP file named "livesearch.php".

The source code in "livesearch.php" searches the XML file for titles that match the search string and returns the results:

load("links.xml"); $x=$xmlDoc->getElementsByTagName('link'); // 从 URL 中获取参数 q 的值 $q=$_GET["q"]; // 如果 q 参数存在则从 xml 文件中查找数据 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="" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } else { $hint=$hint . "
" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } } } } } // 如果没找到则返回 "no suggestion" if ($hint=="") { $response="no suggestion"; } else { $response=$hint; } // 输出结果 echo $response; ?>

If JavaScript sends any text (i.e. strlen($q ) > 0), what will happen:

Load the XML file into a new XML DOM object

Traverse all elements to find the text that matches the text passed by JavaScript</p> <p>Set the correct URL and title in the "$response" variable. If more than one match is found, all matches are added to the variable.</p> <p>If no match is found, set the $response variable to "no suggestion".</p> <p><br></p> </div> </div> <a class="course-btn course_code_header_next" href="//m.sbmmt.com">Continuing Learning</a> </div> <div class="layui-col-md6 editor-box"> <div id="code_spread_shrink"> <div id="code_spread_shrink_show" unselectable="on"> <span>||</span> </div> </div> <div class="editor-tab js-editor-tab"> <div class="editor-left icon-left editor-op"></div> <div class="editor-view"> <ul class="clearfix" id="J_TabType" style="width: 120px; margin-left: 0px;"> <li class="ui-tabs-active"><a href="javascript:;">new file</a></li> </ul> </div> <div class="editor-right icon-right editor-op"></div> </div> <div id="editor-tabs-html" data-filename="index.html" data-lang="php" style="font-size: 16px;height:600px"> <html> <head> <script> function showResult(str) { if (str.length==0) { document.getElementById("livesearch").innerHTML=""; document.getElementById("livesearch").style.border="0px"; return; } if (window.XMLHttpRequest) {// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行 xmlhttp=new XMLHttpRequest(); } else {// 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> <script type="text/javascript" src="/js/jquery.3.5.2.min.m.js"></script> </head><div style="position: fixed;right: 0;top:100px;width: 125px; z-index:2000;"><div ><a target="_blank" rel="nofollow" href="https://www.520xingyun.com/from/188bet.php" ><img width="120px" height="550px" src="https://www.520xingyun.com/images/188_120.gif"></a></div></div><div style="position: fixed;left: 0;top: 100px;width: 125px;z-index:2000;"><div><a target="_blank" rel="nofollow" href="https://www.520xingyun.com/from/188bet.php"><img width="120px" height="550px" src="https://www.520xingyun.com/images/188_120.gif"></a></div></div> <body> <form> <input type="text" size="30" onkeyup="showResult(this.value)"> <div id="livesearch"></div> </form> </body> </html> </div> <div class="editor-btn"> <div class="editor-btn-inner"> <a href="javascript:;" class="code-btn-submit" id="J_Commit" title="submit">submit</a> <a href="javascript:;" class="code-btn-reset " id="J_EditorReset" title="Reset Code">Reset Code</a> <div class="aotorun" style="display: block;"> <input type="checkbox" id="aotoruncheck" title="If selected, modifying the code will automatically take effect. If unchecked, submission is required to take effect"> <label for="aotoruncheck" title="If selected, modifying the code will automatically take effect. If unchecked, submission is required to take effect">Automatic operation</label> </div> </div> </div> </div> <div class="layui-col-md3" id="viewPort-content"></div> <!-- 右侧章节列表 --> </div> </div> <div class="phpSyyBox"> <div class="phpSyyLeft"> <div class="phpSyySyj"> <span class="right"><a href="//m.sbmmt.com/code/355.html" title="Previous section"><b class="icon3"></b>Previous section</a></span> <em></em> <span class="right"><a href="//m.sbmmt.com/code/361.html" title="Next Section"><b class="icon2"></b>Next Section</a></span> </div> <div class="phpSyyQhlx"> <a href="//m.sbmmt.com/course.html" target="_blank" title="Tutorial List"><b></b>Tutorial List</a> </div> <div class="phpSyyHqbz"> <a href="//m.sbmmt.com/wenda.html" target="_blank" title="get help"><b></b>get help</a> </div> <div class="clear"></div> </div> <div class="clear"></div> </div> <div class="layui-main"> <div class="layui-row"> <div class="layui-col-md9"> <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief"> <ul class="layui-tab-title"> <li class="layui-this">Course Recommendations</li> <li id="code_down_li">Courseware download</li> </ul> <div class="layui-tab-content"> <!--推荐课程--> <div class="layui-tab-item layui-show"> <ul class="diy-vodeo diy-li-left php-video-ul"> <li><a href="//m.sbmmt.com/course/1496.html" title=""><img alt="Front-end Vue3 actual combat [handwritten vue project]" src="https://img.php.cn/upload/course/000/000/068/639b12e98e0b5441.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Intermediate</i>Front-end Vue3 actual combat [handwritten vue project]</h3> </div> <div class="php-course-bottom"> <span class="l">2857 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1495.html" title=""><img alt="APIPOST tutorial [Popularization of technical concepts related to network communication]" src="https://img.php.cn/upload/course/000/000/068/63996f34c6c94370.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Elementary</i>APIPOST tutorial [Popularization of technical concepts related to network communication]</h3> </div> <div class="php-course-bottom"> <span class="l">1795 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1494.html" title=""><img alt="Issue 22_Comprehensive actual combat" src="https://img.php.cn/upload/course/000/000/068/6396fa7cacf3b832.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Intermediate</i>Issue 22_Comprehensive actual combat</h3> </div> <div class="php-course-bottom"> <span class="l">5521 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1493.html" title=""><img alt="Issue 22_PHP Programming" src="https://img.php.cn/upload/course/000/000/068/6396fa6f383ca123.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Elementary</i>Issue 22_PHP Programming</h3> </div> <div class="php-course-bottom"> <span class="l">5172 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1492.html" title=""><img alt="Issue 22_Front-end development" src="https://img.php.cn/upload/course/000/000/068/6396fa6140e14800.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Elementary</i>Issue 22_Front-end development</h3> </div> <div class="php-course-bottom"> <span class="l">8713 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1491.html" title=""><img alt="Big data (MySQL) video tutorial full version" src="https://img.php.cn/upload/course/000/000/068/639188999c8fd700.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Intermediate</i>Big data (MySQL) video tutorial full version</h3> </div> <div class="php-course-bottom"> <span class="l">4525 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1490.html" title=""><img alt="Go language tutorial-full of practical information and no nonsense" src="https://img.php.cn/upload/course/000/000/068/638eb9972ff0a840.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Elementary</i>Go language tutorial-full of practical information and no nonsense</h3> </div> <div class="php-course-bottom"> <span class="l">2794 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1489.html" title=""><img alt="GO Language Core Programming Course" src="https://img.php.cn/upload/course/000/000/068/63884a6b0880e953.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Elementary</i>GO Language Core Programming Course</h3> </div> <div class="php-course-bottom"> <span class="l">2814 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1488.html" title=""><img alt="JS advanced and BootStrap learning" src="https://img.php.cn/upload/course/000/000/068/638702e2c8dda719.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Intermediate</i>JS advanced and BootStrap learning</h3> </div> <div class="php-course-bottom"> <span class="l">2563 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1487.html" title=""><img alt="SQL optimization and troubleshooting (MySQL version)" src="https://img.php.cn/upload/course/000/000/068/63846a02a06a2889.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Intermediate</i>SQL optimization and troubleshooting (MySQL version)</h3> </div> <div class="php-course-bottom"> <span class="l">3374 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1486.html" title=""><img alt="Redis+MySQL database interview tutorial" src="https://img.php.cn/upload/course/000/000/068/637c751460263927.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Intermediate</i>Redis+MySQL database interview tutorial</h3> </div> <div class="php-course-bottom"> <span class="l">2963 people are watching</span> </div></a></li> <li><a href="//m.sbmmt.com/course/1485.html" title=""><img alt="Deliver food or learn programming?" src="https://img.php.cn/upload/course/000/000/067/64be34e8b4aa0653.png" height="124" width="100%"> <div class="php-course-intro"> <h3><i>Elementary</i>Deliver food or learn programming?</h3> </div> <div class="php-course-bottom"> <span class="l">5708 people are watching</span> </div></a></li> </ul> </div> <div class="layui-tab-item"> <div id="code_down" style="margin: 0 30px 26px;"> <div class="no_data" style="text-align: center;min-height: 490px;line-height:112px;padding: 190px 0;box-sizing: border-box;color: #e3e3e3;font-size: 20px;"> <div style="background: url(/static/images/lesson_video_lists.png) no-repeat;margin-right: 20px;width: 118px;height: 112px;background-position: 0 -389px;float:left;"></div>The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~ </div> </div> </div> </div> </div> </div> <div class="layui-col-md3 video-right-course"> <div class="layui-col-md12 title"> <p>Students who have watched this course are also learning</p> </div> <div class="layui-col-md12 php-video-img"> <a class="recommend" href="//m.sbmmt.com/course/812.html" target="_blank"><img width="300" height="160" src="https://img.php.cn/upload/course/000/000/041/620debc3eab3f377.jpg" alt="The latest ThinkPHP 5.1 world premiere video tutorial (60 days to become a PHP expert online training course)"><span class="font14">491448 plays</span></a> </div> <div class="layui-col-md12 php-video-list"> <ul> <li><a class="font14 color66" title="Let's briefly talk about starting a business in PHP" href="//m.sbmmt.com/course/1503.html" target="_blank">Let's briefly talk about starting a business in PHP</a></li> <li><a class="font14 color66" title="Quick introduction to web front-end development" href="//m.sbmmt.com/course/901.html" target="_blank">Quick introduction to web front-end development</a></li> <li><a class="font14 color66" title="Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things" href="//m.sbmmt.com/course/836.html" target="_blank">Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things</a></li> <li><a class="font14 color66" title="Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]" href="//m.sbmmt.com/course/902.html" target="_blank">Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]</a></li> <li><a class="font14 color66" title="Login verification and classic message board" href="//m.sbmmt.com/course/866.html" target="_blank">Login verification and classic message board</a></li> <li><a class="font14 color66" title="Computer network knowledge collection" href="//m.sbmmt.com/course/1463.html" target="_blank">Computer network knowledge collection</a></li> <li><a class="font14 color66" title="Quick Start Node.JS Full Version" href="//m.sbmmt.com/course/1462.html" target="_blank">Quick Start Node.JS Full Version</a></li> <li><a class="font14 color66" title="The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]" href="//m.sbmmt.com/course/1455.html" target="_blank">The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]</a></li> <li><a class="font14 color66" title="Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)" href="//m.sbmmt.com/course/1467.html" target="_blank">Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)</a></li> </ul> </div> </div> </div> </div> <div class="phpcn-modal-layer fixed" style="z-index:9004;visibility:visible;opacity:1;display:none;"> <div class="phpcn-modal-title"> <div class="phpcn-modal-draggable dragging"> <span>notes</span> <a href="javascript:void(0)" class="phpcn-icon-close phpcn-modal-close js-modal-close">X</a> </div> </div> <div class="phpcn-modal-inner"> <div class="phpcn-modal-dialog"></div> </div> </div> <div class="phpFoot"> <div class="phpFootIn"> <div class="phpFootCont"> <div class="phpFootLeft"> <dl> <dt> <a href="//m.sbmmt.com/about/us.html" rel="nofollow" target="_blank" title="About us" class="cBlack">About us</a> <a href="//m.sbmmt.com/about/disclaimer.html" rel="nofollow" target="_blank" title="Disclaimer" class="cBlack">Disclaimer</a> <a href="//m.sbmmt.com/update/article_0_1.html" target="_blank" title="Sitemap" class="cBlack">Sitemap</a> <div class="clear"></div> </dt> <dd class="cont1"> php.cn:Public welfare online PHP training,Help PHP learners grow quickly! </dd> </dl> </div> </div> </div> </div> <input type="hidden" id="verifycode" value="/captcha.html"> </body> </html>