PHP-Ajax搜索
PHP-Ajax搜索 Ajax用于与网页和Web服务器通信。下面的示例演示了与Ajax一起使用的搜索字段。 &…
PHP-Ajax搜索
Ajax用于与网页和Web服务器通信。下面的示例演示了与Ajax一起使用的搜索字段。
<style> span { color: green; } </style> <script> function showHint(str) { if (str.length == 0) { document.getElementById("txtHint").innerHTML = ""; return; }else { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("txtHint").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "php_ajax.php?q=" + str, true); xmlhttp.send(); } } </script> <p><b>Search your favourite tutorials:</b></p> <form> <input type="text" onkeyup="showHint(this.value)"> </form> <p>Entered Course name: <span id="txtHint"></span></p>
上面的代码通过使用GET方法打开了一个名为php_ajax.php的文件,因此我们需要在同一目录中创建一个名为php_ajax.php的文件,并且输出将附加txtHint。
php_ajax.php
它包含课程名称数组,并将其值返回到Web浏览器。
<?php // Array with names $a[] = "Android"; $a[] = "B programming language"; $a[] = "C programming language"; $a[] = "D programming language"; $a[] = "euphoria"; $a[] = "F#"; $a[] = "GWT"; $a[] = "HTML5"; $a[] = "ibatis"; $a[] = "Java"; $a[] = "K programming language"; $a[] = "Lisp"; $a[] = "Microsoft technologies"; $a[] = "Networking"; $a[] = "Open Source"; $a[] = "Prototype"; $a[] = "QC"; $a[] = "Restful web services"; $a[] = "Scrum"; $a[] = "Testing"; $a[] = "UML"; $a[] = "VB Script"; $a[] = "Web Technologies"; $a[] = "Xerox Technology"; $a[] = "YQL"; $a[] = "ZOPL"; $q = $_REQUEST["q"]; $hint = ""; if ($q !== "") { $q = strtolower($q); $len = strlen($q); foreach($a as $name) { if (stristr($q, substr($name, 0, $len))) { if ($hint === "") { $hint = $name; }else { $hint .= ", $name"; } } } } echo $hint === "" ? "Please enter a valid course name" : $hint; ??>
它将产生以下结果-
类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!