PHP example - AJAX real-time search

AJAX provides users with a more friendly and interactive search experience.


AJAX Live Search

In the example below , we'll demonstrate a real-time search that provides search results as you type.

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 When, filter the results

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

1.png

The results in the above example are searched in an XML file (links.xml).

Example

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

    PHP中文网(php.cn)  

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 an XMLHttpRequest object

· Create a function to be 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 (including the content of the input box)


XML file

  HTML DOM alt Property  //m.sbmmt.com/htmldom/prop_img_alt.asp    HTML DOM height Property  //m.sbmmt.com/htmldom/prop_img_height.asp    HTML a tag //m.sbmmt.com/tags/tag_a.asp   HTML br tag //m.sbmmt.com/tags/tag_br.asp   JavaScript Date() Method //m.sbmmt.com/jsref/jsref_date.asp   JavaScript anchor() Method //m.sbmmt.com/jsref/jsref_anchor.asp  

PHP file

The above paragraph is called through JavaScript The server page 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>Program results display:</p> <p><img src="https://img.php.cn/upload/image/725/460/548/1476086311837473.png" title="1476086311837473.png" alt="3.png"></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"> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>PHP中文网(php.cn)

submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!