首页 > 代码库 > 12. php and Ajax
12. php and Ajax
1. php and Ajax
1.a
html code
<html><head><script>function showHint(str) { if (str.length == 0) { document.getElementById("txtHint").innerHTML = ""; return; } else { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("txtHint").innerHTML = this.responseText; } }; xmlhttp.open("GET", "gethint.php?q=" + str, true); xmlhttp.send(); }}</script></head><body><p><b>Start typing a name in the input field below:</b></p><form> First name: <input type="text" onkeyup="showHint(this.value)"></form><p>Suggestions: <span id="txtHint"></span></p></body></html>
1.b
gethint.php
<?php// Array with names$a[] = "Anna";$a[] = "Brittany";$a[] = "Cinderella";$a[] = "Diana";$a[] = "Eva";$a[] = "Fiona";$a[] = "Gunda";$a[] = "Hege";$a[] = "Inga";$a[] = "Johanna";$a[] = "Kitty";$a[] = "Linda";$a[] = "Nina";$a[] = "Ophelia";$a[] = "Petunia";$a[] = "Amanda";$a[] = "Raquel";$a[] = "Cindy";$a[] = "Doris";$a[] = "Eve";$a[] = "Evita";$a[] = "Sunniva";$a[] = "Tove";$a[] = "Unni";$a[] = "Violet";$a[] = "Liza";$a[] = "Elizabeth";$a[] = "Ellen";$a[] = "Wenche";$a[] = "Vicky";// get the q parameter from URL$q = $_REQUEST["q"];$hint = "";// lookup all hints from array if $q is different from "" 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"; } } }}// Output "no suggestion" if no hint was found or output correct values echo $hint === "" ? "no suggestion" : $hint;?>
12. php and Ajax
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。