AJAX PHP 实例
AJAX 用于创建更具交互性的应用程序。
AJAX PHP 实例
下面的例子演示:当用户在输入字段中键入字符时,网页如何与 web 服务器进行通信:
实例
在下面的输入字段中键入名称:
Suggestions:
First name:
实例说明
在上面的例子中,当用户在输入字段中键入字符时,会执行名为 showHint()
的函数。
此函数被 onkeyup
事件触发。
下面是 HTML 代码:
实例
<html>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<p>Suggestions: <span id="txtHint"></span></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<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>
</body>
</html>
亲自试一试 »
<body>
<p><b>Start typing a name in the input field below:</b></p>
<p>Suggestions: <span id="txtHint"></span></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<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>
</body>
</html>
源代码解析:
首先,检查输入字段是否为空(str.length == 0),如果是,清空 txtHint 占位符的内容并退出函数。
不过,如果输入字段不为空,则进行如下:
- 创建 XMLHttpRequest 对象
- 创建当服务器响应就绪时执行的函数
- 发送请求到服务器上的 PHP 文件(gethint.php)
- 请注意添加到 gethint.php 的 q 参数
- str 变量保存了输入字段的内容
PHP 文件 - "gethint.php"
这个 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";
// 从 URL 获取 q 参数
$q = $_REQUEST["q"];
$hint = "";
// 如果 $q 与 "" 不同,则从数组中查找所有提示
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";
}
}
}
}
// 如果没有找到提示,则输出 "no suggestion" 或输出正确的值
echo $hint === "" ? "no suggestion" : $hint;
?>
// 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";
// 从 URL 获取 q 参数
$q = $_REQUEST["q"];
$hint = "";
// 如果 $q 与 "" 不同,则从数组中查找所有提示
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";
}
}
}
}
// 如果没有找到提示,则输出 "no suggestion" 或输出正确的值
echo $hint === "" ? "no suggestion" : $hint;
?>