- PHP 教程
- PHP - 首頁
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混淆
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc 和 Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期與時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 恆等比較運算子
- PHP 控制語句
- PHP - 決策制定
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件的 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - 特性
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - Final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET 和 POST
- PHP - 檔案上傳
- PHP - Cookie
- PHP - Session
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 淨化輸入
- PHP - Post-Redirect-Get (PRG)
- PHP - 快閃記憶體訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - Bug 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - Core PHP 與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 期望
- PHP - Use 語句
- PHP - 整數除法
- PHP - 已棄用的特性
- PHP - 已刪除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
PHP - AJAX 自動完成搜尋
自動完成功能是一種輸入提示機制,當用戶在提供的搜尋框中輸入資料時,它會顯示輸入建議。它也稱為即時搜尋,因為它會對使用者的輸入做出反應。在本例中,我們將使用 PHP 中的 AJAX 和 XML 解析器來演示自動完成文字框的使用。
此應用程式具有三個主要組成部分:
XML 文件
JavaScript 程式碼
PHP 中的 XML 解析器
現在讓我們詳細討論這三個組成部分:
XML 文件
將以下 XML 指令碼儲存為“autocomplete.xml”到文件根目錄資料夾
<?xml version = "1.0" encoding = "utf-8"?>
<pages>
<link>
<title>android</title>
<url>https://tutorialspoint.tw/android/index.htm</url>
</link>
<link>
<title>Java</title>
<url>https://tutorialspoint.tw/java/index.htm</url>
</link>
<link>
<title>CSS </title>
<url>https://tutorialspoint.tw/css/index.htm</url>
</link>
<link>
<title>angularjs</title>
<url>https://tutorialspoint.tw/angularjs/index.htm </url>
</link>
<link>
<title>hadoop</title>
<url>https://tutorialspoint.tw/hadoop/index.htm </url>
</link>
<link>
<title>swift</title>
<url>https://tutorialspoint.tw/swift/index.htm </url>
</link>
<link>
<title>ruby</title>
<url>https://tutorialspoint.tw/ruby/index.htm </url>
</link>
<link>
<title>nodejs</title>
<url>https://tutorialspoint.tw/nodejs/index.htm </url>
</link>
</pages>
JavaScript 程式碼
以下指令碼呈現一個文字欄位,供使用者輸入他選擇的課程名稱。每次按鍵都會呼叫一個 JavaScript 函式,並將輸入值透過 GET 方法傳遞到伺服器端的 PHP 指令碼。伺服器的響應會非同步渲染。
將此程式碼儲存為“index.php”。
<html>
<head>
<script>
function showResult(str) {
if (str.length == 0) {
document.getElementById("livesearch").innerHTML = "";
document.getElementById("livesearch").style.border = "0px";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("livesearch").innerHTML = xmlhttp.responseText;
document.getElementById("livesearch").style.border = "1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<h2>Enter Course Name</h2>
<input type = "text" size = "30" onkeyup = "showResult(this.value)">
<div id = "livesearch"></div>
<a href = "https://tutorialspoint.tw">More Details</a>
</form>
</body>
</html>
PHP 中的 XML 解析器
這是伺服器上的 PHP 指令碼。它解析給定的 XML 源文件,讀取輸入欄位中輸入的字元,在解析的 XNL 物件中搜索它,併發送回響應。
將以下程式碼儲存為“livesearch.php”。
<?php
$xml_doc = new DOMDocument();
$xml_doc->load('autocomplete.xml');
$x=$xml_doc->getElementsByTagName('link');
$q = $_GET['q'];
$result = '';
foreach($x as $node) {
if (stripos("{$node->nodeValue}", $q) !== false) {
$result .= "{$node->nodeValue}";
}
}
// Set $response to "No records found." in case no hint was found
// or the values of the matching values
if ($result == '')
$result = 'No records found.';
// show the results or "No records found."
echo $result;
?>
在 XAMPP 伺服器執行的情況下,訪問“https:///index.php”,瀏覽器會顯示一個輸入文字欄位。對於在其中鍵入的每個字元,相關的建議都會顯示在其下方。
廣告