選單
×
   ❮     
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 提供支援