{"id":3145,"date":"2025-03-22T11:09:46","date_gmt":"2025-03-22T03:09:46","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3145"},"modified":"2025-03-22T11:09:46","modified_gmt":"2025-03-22T03:09:46","slug":"php-%e5%ae%9e%e4%be%8b-ajax-%e5%ae%9e%e6%97%b6%e6%90%9c%e7%b4%a2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-%e5%ae%9e%e4%be%8b-ajax-%e5%ae%9e%e6%97%b6%e6%90%9c%e7%b4%a2\/","title":{"rendered":"PHP \u5b9e\u4f8b &#8211; AJAX \u5b9e\u65f6\u641c\u7d22"},"content":{"rendered":"\n<p><strong>AJAX \u5b9e\u65f6\u641c\u7d22<\/strong>\u662f\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u4e00\u79cd\u4ea4\u4e92\u65b9\u5f0f\uff0c\u80fd\u591f\u5728\u7528\u6237\u8f93\u5165\u65f6\u52a8\u6001\u5730\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u6570\u636e\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u7684\u7ed3\u679c\u5b9e\u65f6\u66f4\u65b0\u9875\u9762\u5185\u5bb9\uff0c\u800c\u65e0\u9700\u5237\u65b0\u6574\u4e2a\u9875\u9762\u3002\u4f7f\u7528 <strong>AJAX<\/strong> \u548c <strong>PHP<\/strong>\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5b9e\u65f6\u641c\u7d22\u529f\u80fd\uff0c\u5f53\u7528\u6237\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u5b57\u7b26\u65f6\uff0c\u9875\u9762\u4f1a\u6839\u636e\u8f93\u5165\u52a8\u6001\u663e\u793a\u5339\u914d\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5e94\u7528\u573a\u666f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u52a8\u6001\u641c\u7d22\u6570\u636e\u5e93\u4e2d\u7684\u5185\u5bb9\uff08\u5982\u6587\u7ae0\u3001\u7528\u6237\u4fe1\u606f\u3001\u5546\u54c1\u5217\u8868\u7b49\uff09\u3002<\/li>\n\n\n\n<li>\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u51cf\u5c11\u9875\u9762\u52a0\u8f7d\u65f6\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u5b9e\u73b0 AJAX \u5b9e\u65f6\u641c\u7d22\u7684\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u7528\u6237\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u67e5\u8be2\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong>AJAX \u8bf7\u6c42<\/strong>\uff1aJavaScript \u4f7f\u7528 <strong>AJAX<\/strong> \u5411\u670d\u52a1\u5668\u53d1\u9001\u8f93\u5165\u7684\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>PHP \u5904\u7406\u8bf7\u6c42<\/strong>\uff1aPHP \u63a5\u6536\u5230\u641c\u7d22\u8bf7\u6c42\uff0c\u67e5\u8be2\u6570\u636e\u5e93\u5e76\u8fd4\u56de\u76f8\u5173\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u6570\u636e<\/strong>\uff1a\u670d\u52a1\u5668\u8fd4\u56de\u5339\u914d\u7684\u6570\u636e\uff0c\u901a\u5e38\u662f JSON \u683c\u5f0f\u6216\u8005 HTML \u683c\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u66f4\u65b0\u9875\u9762<\/strong>\uff1aJavaScript \u4f7f\u7528\u63a5\u6536\u5230\u7684\u6570\u636e\uff0c\u66f4\u65b0\u9875\u9762\u4e0a\u7684\u641c\u7d22\u7ed3\u679c\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u793a\u4f8b\uff1aAJAX \u5b9e\u65f6\u641c\u7d22\u4e0e PHP \u5b9e\u73b0<\/strong><\/h2>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u7528\u6237\u4fe1\u606f\u7684\u6570\u636e\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8f93\u5165\u7528\u6237\u540d\u6765\u5b9e\u65f6\u641c\u7d22\u5339\u914d\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1. \u6570\u636e\u5e93\u8bbe\u7f6e<\/strong><\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e00\u4e2a\u6570\u636e\u5e93 <code>ajax_search_example<\/code> \u548c\u4e00\u4e2a\u7528\u6237\u8868 <code>users<\/code>\uff0c\u5982\u4ee5\u4e0b SQL \u8bed\u53e5\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u521b\u5efa\u6570\u636e\u5e93\nCREATE DATABASE ajax_search_example;\n\n-- \u9009\u62e9\u6570\u636e\u5e93\nUSE ajax_search_example;\n\n-- \u521b\u5efa\u7528\u6237\u8868\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(50) NOT NULL,\n    email VARCHAR(100) NOT NULL\n);\n\n-- \u63d2\u5165\u4e00\u4e9b\u6d4b\u8bd5\u6570\u636e\nINSERT INTO users (username, email) VALUES ('Alice', 'alice@example.com');\nINSERT INTO users (username, email) VALUES ('Bob', 'bob@example.com');\nINSERT INTO users (username, email) VALUES ('Charlie', 'charlie@example.com');\nINSERT INTO users (username, email) VALUES ('David', 'david@example.com');\nINSERT INTO users (username, email) VALUES ('Eve', 'eve@example.com');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2. HTML \u9875\u9762\uff1a\u641c\u7d22\u6846\u548c\u7ed3\u679c\u5c55\u793a<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5305\u542b\u641c\u7d22\u6846\u548c\u641c\u7d22\u7ed3\u679c\u5c55\u793a\u533a\u57df\u7684 HTML \u9875\u9762\uff0c\u7528\u6237\u8f93\u5165\u65f6\u4f1a\u89e6\u53d1 AJAX \u8bf7\u6c42\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;PHP - AJAX \u5b9e\u65f6\u641c\u7d22&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;h1&gt;\u5b9e\u65f6\u641c\u7d22&lt;\/h1&gt;\n    &lt;input type=\"text\" id=\"searchBox\" placeholder=\"\u641c\u7d22\u7528\u6237\u540d...\" autocomplete=\"off\"&gt;\n    &lt;div id=\"searchResults\"&gt;&lt;\/div&gt;\n\n    &lt;script&gt;\n        \/\/ JavaScript\uff1a\u76d1\u542c\u7528\u6237\u8f93\u5165\uff0c\u53d1\u9001 AJAX \u8bf7\u6c42\n        document.getElementById('searchBox').addEventListener('input', function() {\n            var query = this.value;  \/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\n\n            if (query.length &gt; 0) { \/\/ \u5982\u679c\u8f93\u5165\u7684\u5185\u5bb9\u957f\u5ea6\u5927\u4e8e0\uff0c\u624d\u53d1\u9001\u8bf7\u6c42\n                var xhr = new XMLHttpRequest();\n                xhr.open('GET', 'search.php?q=' + encodeURIComponent(query), true); \/\/ \u5411 PHP \u53d1\u9001 GET \u8bf7\u6c42\n                xhr.onreadystatechange = function() {\n                    if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {\n                        \/\/ \u66f4\u65b0\u641c\u7d22\u7ed3\u679c\n                        document.getElementById('searchResults').innerHTML = xhr.responseText;\n                    }\n                };\n                xhr.send(); \/\/ \u53d1\u9001\u8bf7\u6c42\n            } else {\n                document.getElementById('searchResults').innerHTML = ''; \/\/ \u5982\u679c\u6ca1\u6709\u8f93\u5165\uff0c\u6e05\u7a7a\u641c\u7d22\u7ed3\u679c\n            }\n        });\n    &lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2.3. PHP \u6587\u4ef6\uff08search.php\uff09\uff1a\u5904\u7406\u641c\u7d22\u8bf7\u6c42<\/strong><\/h2>\n\n\n\n<p><code>search.php<\/code> \u6587\u4ef6\u7528\u4e8e\u63a5\u6536\u7528\u6237\u7684\u641c\u7d22\u8bf7\u6c42\uff0c\u67e5\u8be2\u6570\u636e\u5e93\u5e76\u8fd4\u56de\u5339\u914d\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ \u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"ajax_search_example\";\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u83b7\u53d6\u641c\u7d22\u5173\u952e\u8bcd\n$query = $_GET&#91;'q'];\n\n\/\/ \u9632\u6b62 SQL \u6ce8\u5165\n$query = $conn-&gt;real_escape_string($query);\n\n\/\/ \u6267\u884c\u641c\u7d22\u67e5\u8be2\n$sql = \"SELECT username, email FROM users WHERE username LIKE '%$query%' LIMIT 5\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u68c0\u67e5\u662f\u5426\u6709\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f93\u51fa\u6bcf\u884c\u6570\u636e\n    while ($row = $result-&gt;fetch_assoc()) {\n        echo \"&lt;p&gt;&lt;strong&gt;\" . $row&#91;'username'] . \"&lt;\/strong&gt; - \" . $row&#91;'email'] . \"&lt;\/p&gt;\";\n    }\n} else {\n    echo \"&lt;p&gt;\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u7ed3\u679c\u3002&lt;\/p&gt;\";\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4. \u5de5\u4f5c\u6d41\u7a0b<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u8f93\u5165<\/strong>\uff1a\u7528\u6237\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u5185\u5bb9\uff0c\u89e6\u53d1 <code>input<\/code> \u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u53d1\u9001 AJAX \u8bf7\u6c42<\/strong>\uff1aJavaScript \u4f7f\u7528 <strong>XMLHttpRequest<\/strong> \u5411 <code>search.php<\/code> \u53d1\u9001\u8bf7\u6c42\uff0c\u5e26\u4e0a\u7528\u6237\u8f93\u5165\u7684\u641c\u7d22\u5173\u952e\u8bcd\u3002<\/li>\n\n\n\n<li><strong>PHP \u67e5\u8be2\u6570\u636e\u5e93<\/strong>\uff1a<code>search.php<\/code> \u4ece\u6570\u636e\u5e93\u67e5\u8be2\u4e0e\u7528\u6237\u8f93\u5165\u5339\u914d\u7684\u7ed3\u679c\uff08\u4f7f\u7528 SQL <code>LIKE<\/code> \u64cd\u4f5c\u7b26\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u641c\u7d22\u7ed3\u679c<\/strong>\uff1aPHP \u5c06\u67e5\u8be2\u5230\u7684\u7528\u6237\u5217\u8868\u4ee5 HTML \u683c\u5f0f\u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/li>\n\n\n\n<li><strong>\u66f4\u65b0\u9875\u9762\u5185\u5bb9<\/strong>\uff1aJavaScript \u5c06\u8fd4\u56de\u7684 HTML \u63d2\u5165\u5230\u9875\u9762\u4e0a\u7684 <code>#searchResults<\/code> \u5143\u7d20\u4e2d\uff0c\u663e\u793a\u5b9e\u65f6\u641c\u7d22\u7ed3\u679c\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5. \u793a\u4f8b\uff1a\u8fd4\u56de\u7684 HTML \u683c\u5f0f<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u7528\u6237\u8f93\u5165 <code>Al<\/code>\uff0cPHP \u8fd4\u56de\u7684\u7ed3\u679c\u53ef\u80fd\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p&gt;&lt;strong&gt;Alice&lt;\/strong&gt; - alice@example.com&lt;\/p&gt;\n&lt;p&gt;&lt;strong&gt;Alfred&lt;\/strong&gt; - alfred@example.com&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4f18\u5316\u4e0e\u6539\u8fdb<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9632\u6b62\u8fc7\u591a\u8bf7\u6c42<\/strong>\uff1a\u5f53\u7528\u6237\u5feb\u901f\u8f93\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u9632\u6296\uff08debounce\uff09\u6280\u672f\uff0c\u5ef6\u8fdf\u8bf7\u6c42\u53d1\u9001\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u3002 \u793a\u4f8b\uff1a\u53ef\u4ee5\u901a\u8fc7 <code>setTimeout<\/code> \u548c <code>clearTimeout<\/code> \u6765\u5b9e\u73b0\u9632\u6296\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   let timer;\n   document.getElementById('searchBox').addEventListener('input', function() {\n       clearTimeout(timer);\n       timer = setTimeout(function() {\n           var query = document.getElementById('searchBox').value;\n           if (query.length &gt; 0) {\n               \/\/ \u53d1\u9001 AJAX \u8bf7\u6c42\n               var xhr = new XMLHttpRequest();\n               xhr.open('GET', 'search.php?q=' + encodeURIComponent(query), true);\n               xhr.onreadystatechange = function() {\n                   if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {\n                       document.getElementById('searchResults').innerHTML = xhr.responseText;\n                   }\n               };\n               xhr.send();\n           }\n       }, 500); \/\/ 500 \u6beb\u79d2\u540e\u53d1\u9001\u8bf7\u6c42\n   });<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u5206\u9875<\/strong>\uff1a\u5982\u679c\u641c\u7d22\u7ed3\u679c\u975e\u5e38\u591a\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5206\u9875\u663e\u793a\uff0c\u4ee5\u51cf\u5c11\u6bcf\u6b21\u8bf7\u6c42\u8fd4\u56de\u7684\u6570\u636e\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5b89\u5168\u6027<\/strong>\uff1a\u8bf7\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7684\u5b89\u5168\u6027\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u3001XSS \u7b49\u653b\u51fb\u3002\u4e0a\u9762\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86 <code>$conn->real_escape_string($query)<\/code> \u6765\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/li>\n\n\n\n<li><strong>\u54cd\u5e94\u683c\u5f0f<\/strong>\uff1a\u4f60\u53ef\u4ee5\u9009\u62e9\u8fd4\u56de <strong>JSON<\/strong> \u683c\u5f0f\u7684\u6570\u636e\uff0c\u800c\u4e0d\u662f HTML\uff0c\u4ee5\u4fbf\u66f4\u7075\u6d3b\u5730\u5904\u7406\u524d\u7aef\u6e32\u67d3\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u7ed3\u5408 <strong>AJAX<\/strong> \u548c <strong>PHP<\/strong> \u5b9e\u73b0\u5b9e\u65f6\u641c\u7d22\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u907f\u514d\u9875\u9762\u5237\u65b0\u5e76\u52a0\u5feb\u6570\u636e\u52a0\u8f7d\u901f\u5ea6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u3001\u589e\u52a0\u5b89\u5168\u6027\uff0c\u63d0\u5347\u641c\u7d22\u7684\u51c6\u786e\u5ea6\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AJAX \u5b9e\u65f6\u641c\u7d22\u662f\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u4e00\u79cd\u4ea4\u4e92\u65b9\u5f0f\uff0c\u80fd\u591f\u5728\u7528\u6237\u8f93\u5165\u65f6\u52a8\u6001\u5730\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u6570\u636e\uff0c\u5e76\u6839\u636e\u8fd4\u56de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3146,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3145"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3145\/revisions"}],"predecessor-version":[{"id":3147,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3145\/revisions\/3147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3146"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}