菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP 操作指南 W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA 生成式 AI SCIPY AWS 网络安全 数据科学
     ❯   

XML 教程

XML 首页 XML 简介 XML 用途 XML 树 XML 语法 XML 元素 XML 属性 XML 命名空间 XML 显示 XML HttpRequest XML 解析器 XML DOM XML XPath XML XSLT XML XQuery XML XLink XML 验证器 XML DTD XML Schema XML 服务器 XML 示例 XML 测验 XML 证书

XML AJAX

AJAX Introduction AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

XML DOM

DOM Introduction DOM Nodes DOM Accessing DOM Node Info DOM Node List DOM Traversing DOM Navigating DOM Get Values DOM Change Nodes DOM Remove Nodes DOM Replace Nodes DOM Create Nodes DOM Add Nodes DOM Clone Nodes DOM Examples

XPath 教程

XPath Introduction XPath Nodes XPath Syntax XPath Axes XPath Operators XPath Examples

XSLT 教程

XSLT Introduction XSL Languages XSLT Transform XSLT <template> XSLT <value-of> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <choose> XSLT Apply XSLT on the Client XSLT on the Server XSLT Edit XML XSLT Examples

XQuery 教程

XQuery Introduction XQuery Example XQuery FLWOR XQuery HTML XQuery Terms XQuery Syntax XQuery Add XQuery Select XQuery Functions

XML DTD

DTD Introduction DTD Building Blocks DTD Elements DTD Attributes DTD Elements vs Attr DTD Entities DTD Examples

XSD Schema

XSD 简介 XSD 操作指南 XSD <schema> XSD 元素 XSD 属性 XSD 限制 XSD 复杂元素 XSD 空 XSD 仅元素 XSD 仅文本 XSD 混合 XSD 指示符 XSD <any> XSD <anyAttribute> XSD 替换 XSD 示例

XSD Data Types

XSD String XSD Date/Time XSD Numeric XSD Misc XSD Reference

Web Services

XML Services XML WSDL XML SOAP XML RDF XML RSS

参考手册

DOM 节点类型 DOM 节点 DOM 节点列表 DOM NamedNodeMap DOM Document DOM Element DOM Attribute DOM Text DOM CDATA DOM Comment DOM XMLHttpRequest DOM Parser XSLT 元素 XSLT/XPath 函数

AJAX PHP 示例


AJAX 用于创建更具交互性的应用程序。


AJAX PHP 示例

以下示例演示了当用户在输入字段中键入字符时,网页如何与 Web 服务器通信

示例

在下面的输入字段中开始输入姓名

  建议:



示例解释

在上面的示例中,当用户在输入字段中键入一个字符时,会执行一个名为 "showHint()" 的函数。

该函数由 onkeyup 事件触发。

这是 HTML 代码

示例

<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>在下面的输入框中输入姓名:</b></p>
<form>
名: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>建议: <span id="txtHint"></span></p>
</body>
</html>
自己动手试一试 »

代码解释

首先,检查输入字段是否为空(str.length == 0)。如果是,则清除 txtHint 占位符的内容并退出函数。

但是,如果输入字段不为空,则执行以下操作:

  • 创建一个 XMLHttpRequest 对象
  • 创建服务器响应准备好时要执行的函数
  • 将请求发送到服务器上的 PHP 文件 (gethint.php)
  • 请注意,q 参数已添加到 gethint.php?q="+str
  • str 变量保存着输入字段的内容


PHP 文件 - "gethint.php"

PHP 文件会检查姓名数组,并将对应的姓名返回给浏览器。

<?php
// 名称数组
$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 === "" ? "没有建议" : $hint;
?>

×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持