PHP complete se... LOGIN
PHP complete self-study manual
author:php.cn update time:2022-04-15 13:53:54

AJAX real-time search



AJAX provides users with a friendlier and more 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 data is entered, matching results will be displayed

  • Filter the results as you continue to type data

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

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

The results in the above example are found 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 contents of the livesearch placeholder and exit the function.

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

  • Create XMLHttpRequest object

  • Create Function executed when the server response is ready

  • Sends a request to a file on the server

  • Please note the parameter (q) added to the end of the URL (Including the content of the input box)


PHP file

The server page called through JavaScript above 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), this will happen:

  • Loading the XML file into a new XML DOM object

  • Loop through all elements to find one that matches the text passed by the JavaScript</p></li> <li><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></li> <li><p>If no match is found, set the $response variable to "no suggestion".</p></li> </ul> <p><strong>Recommended related video tutorials</strong>: "<a href="//m.sbmmt.com/m/course/list/25.html" target="_blank">AJAX Tutorial</a>"<a href="//m.sbmmt.com/m/course/list/25.html" _src="//m.sbmmt.com/m/course/list/25.html">//m.sbmmt.com/course/list/ 25.html</a></p> </div> </div> </div> </div> <div class="previous-next-links"> <div class="previous-design-link"> ← <a href="//m.sbmmt.com/m/php/php-ajax-xml.html" rel="prev">AJAX XML</a> </div> <div class="next-design-link"> <a href="//m.sbmmt.com/m/php/php-ajax-rss-reader.html" rel="next">AJAX RSS Reader</a>→ </div> </div> <div class="sidebar-box ad-box ad-box-large"> <div class="ad-336280"></div> </div> </div> </div> </div> </div> <div id="codeMark"></div> <div id="codeMain"> <div class="left"> <div id="codeEditor"></div> <div class="editor-btn"> <div class="editor-btn-inner"> <a href="javascript:;" class="code-btn-submit r" id="J_Commit" title="submit">submit</a> </div> </div> </div> <div class="right"> <div id="codeResult"></div> </div> <div id="close"></div> </div> <footer> <a href="//m.sbmmt.com/m/"><i class="layui-icon layui-icon-home "></i>Home</a> <a href="//m.sbmmt.com/m/course.html"><i class="layui-icon layui-icon-play"></i>Videos</a> <a href="//m.sbmmt.com/m/wenda.html"><i class="layui-icon layui-icon-code-circle"></i>Q&A</a> <a href="//m.sbmmt.com/m/login"><i class="layui-icon layui-icon-username"></i>My</a> </footer> <div class="right_menu"> <div class="right_menu_con"> <h2 class="hjclass-txt"><i class="layui-icon layui-icon-shrink-right hjclass-txt-i"></i>php.cn</h2> <div class="menu-list"> <a href="//m.sbmmt.com/m/"><span class="item-icon item-1"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb65f99ae2304.png" alt="Home"></span>Home</a> <a href="//m.sbmmt.com/m/course.html"><span class="item-icon item-7"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb82bd09ad628.png" alt="Course"></span>Course</a> <a href="//m.sbmmt.com/m/article.html"><span class="item-icon item-4"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb7e111b3b425.png" alt="Article"></span>Article</a> <a href="//m.sbmmt.com/m/wenda.html"><span class="item-icon item-2"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb83f04e24328.png" alt="Q&A"></span>Q&A</a> <a href="//m.sbmmt.com/m/dic.html"><span class="item-icon item-9"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb85047b25758.png" alt="Dictionary"></span>Dictionary</a> <a href="//m.sbmmt.com/m/course/type/99.html"><span class="item-icon item-3"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb8736ea90300.png" alt="Manual"></span>Manual</a> <a href="//m.sbmmt.com/m/xiazai/"><span class="item-icon item-5"><img src="//m.sbmmt.com/m/static/images/ico/resources.png" alt="Download"></span>Download</a> <a href="//m.sbmmt.com/m/search"><span class="item-icon item-6"><img src="//m.sbmmt.com/img/upload/article/000/000/003/5bffb880b460a204.png" alt="search"></span>search</a> <a href="//m.sbmmt.com/m/app/"><span class="item-icon item-6"><img src="//m.sbmmt.com/img/upload/article/000/000/003/608bbfa30d9cc398.png" style="width:35px;height:35px;" alt="APPDownload"></span>APPDownload</a> </div> </div> </div> </body> </html>