{"id":3142,"date":"2025-03-22T10:53:14","date_gmt":"2025-03-22T02:53:14","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3142"},"modified":"2025-03-22T10:53:14","modified_gmt":"2025-03-22T02:53:14","slug":"php-%e5%ae%9e%e4%be%8b-ajax-%e4%b8%8e-xml","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-%e5%ae%9e%e4%be%8b-ajax-%e4%b8%8e-xml\/","title":{"rendered":"PHP \u5b9e\u4f8b &#8211; AJAX \u4e0e XML"},"content":{"rendered":"\n<p><strong>AJAX<\/strong> \u548c <strong>XML<\/strong>\uff08\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6280\u672f\u7ec4\u5408\uff0c\u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\u5e38\u5e38\u88ab\u7528\u6765\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5\u4e0b\u5f02\u6b65\u5730\u4e0e\u670d\u52a1\u5668\u4ea4\u6362\u6570\u636e\u5e76\u66f4\u65b0\u7f51\u9875\u5185\u5bb9\u3002\u867d\u7136\u8fd1\u5e74\u6765 <strong>JSON<\/strong>\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u9010\u6e10\u53d6\u4ee3\u4e86 XML\uff0c\u4f46 XML \u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e0b\u4ecd\u7136\u6709\u5e7f\u6cdb\u7684\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u7ed3\u5408 <strong>AJAX<\/strong> \u548c <strong>PHP<\/strong> \u4f7f\u7528 <strong>XML<\/strong> \u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u901a\u8fc7 AJAX \u5411 PHP \u53d1\u9001\u8bf7\u6c42\uff0cPHP \u8fd4\u56de XML \u683c\u5f0f\u7684\u6570\u636e\uff0cJavaScript \u89e3\u6790\u8fd9\u4e9b XML \u6570\u636e\u5e76\u66f4\u65b0\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 AJAX \u53d1\u9001\u8bf7\u6c42\u5e76\u63a5\u6536 XML \u6570\u636e<\/strong><\/h2>\n\n\n\n<p><strong>AJAX<\/strong> \u548c <strong>XML<\/strong> \u7ed3\u5408\u4f7f\u7528\u7684\u6d41\u7a0b\u901a\u5e38\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u89e6\u53d1\u4e8b\u4ef6<\/strong>\uff1a\u4f8b\u5982\u70b9\u51fb\u6309\u94ae\u6216\u5176\u4ed6\u4ea4\u4e92\u52a8\u4f5c\uff0cJavaScript \u4f7f\u7528 <strong>AJAX<\/strong> \u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u3002<\/li>\n\n\n\n<li><strong>PHP \u5904\u7406\u8bf7\u6c42<\/strong>\uff1aPHP \u63a5\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u67e5\u8be2\u6570\u636e\u5e93\u6216\u8fdb\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u5e76\u751f\u6210 <strong>XML<\/strong> \u683c\u5f0f\u7684\u54cd\u5e94\u3002<\/li>\n\n\n\n<li><strong>JavaScript \u89e3\u6790 XML \u6570\u636e<\/strong>\uff1aJavaScript \u4f7f\u7528 <strong>XMLHttpRequest<\/strong> \u5bf9\u8c61\u63a5\u6536 PHP \u8fd4\u56de\u7684 XML \u6570\u636e\uff0c\u89e3\u6790\u5e76\u5c06\u5176\u63d2\u5165\u5230 HTML \u9875\u9762\u4e2d\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 \u548c PHP \u4e0e XML<\/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\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 <strong>AJAX<\/strong> \u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c\u670d\u52a1\u5668\u8fd4\u56de <strong>XML<\/strong> \u6570\u636e\uff0cJavaScript \u89e3\u6790\u5e76\u52a8\u6001\u663e\u793a\u8fd9\u4e9b\u6570\u636e\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\u5728 MySQL \u4e2d\u6709\u4e00\u4e2a\u6570\u636e\u5e93\u548c\u7528\u6237\u8868\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u521b\u5efa\u6570\u636e\u5e93\nCREATE DATABASE ajax_xml_example;\n\n-- \u9009\u62e9\u6570\u636e\u5e93\nUSE ajax_xml_example;\n\n-- \u521b\u5efa\u7528\u6237\u4fe1\u606f\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');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2. HTML \u9875\u9762\uff1aAJAX \u8bf7\u6c42\u89e6\u53d1<\/strong><\/h3>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 HTML \u9875\u9762\uff0c\u7528\u6237\u70b9\u51fb\u6309\u94ae\u540e\uff0c\u901a\u8fc7 <strong>AJAX<\/strong> \u8bf7\u6c42\u83b7\u53d6\u7528\u6237\u6570\u636e\u5e76\u901a\u8fc7 <strong>XML<\/strong> \u683c\u5f0f\u663e\u793a\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 \u4e0e XML \u793a\u4f8b&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n    &lt;h1&gt;\u7528\u6237\u5217\u8868&lt;\/h1&gt;\n    &lt;button id=\"loadUsersButton\"&gt;\u52a0\u8f7d\u7528\u6237&lt;\/button&gt;\n    &lt;div id=\"userList\"&gt;&lt;\/div&gt;\n\n    &lt;script&gt;\n        \/\/ JavaScript: \u76d1\u542c\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\uff0c\u53d1\u9001 AJAX \u8bf7\u6c42\n        document.getElementById('loadUsersButton').addEventListener('click', function() {\n            var xhr = new XMLHttpRequest();\n            xhr.open('GET', 'fetch_users.php', true); \/\/ \u5411 PHP \u53d1\u9001 GET \u8bf7\u6c42\n            xhr.onreadystatechange = function() {\n                if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {\n                    \/\/ \u89e3\u6790\u8fd4\u56de\u7684 XML \u6570\u636e\n                    var xml = xhr.responseXML;\n                    var users = xml.getElementsByTagName('user');\n                    var output = '';\n\n                    \/\/ \u904d\u5386\u6240\u6709\u7528\u6237\u8282\u70b9\u5e76\u6784\u5efa HTML \u5185\u5bb9\n                    for (var i = 0; i &lt; users.length; i++) {\n                        var username = users&#91;i].getElementsByTagName('username')&#91;0].textContent;\n                        var email = users&#91;i].getElementsByTagName('email')&#91;0].textContent;\n\n                        output += \"&lt;p&gt;\u7528\u6237\u540d: \" + username + \" - \u7535\u5b50\u90ae\u4ef6: \" + email + \"&lt;\/p&gt;\";\n                    }\n\n                    \/\/ \u66f4\u65b0\u9875\u9762\u5185\u5bb9\n                    document.getElementById('userList').innerHTML = output;\n                }\n            };\n            xhr.send(); \/\/ \u53d1\u9001\u8bf7\u6c42\n        });\n    &lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3. PHP \u6587\u4ef6\uff08fetch_users.php\uff09\uff1a\u751f\u6210 XML \u54cd\u5e94<\/strong><\/h3>\n\n\n\n<p>PHP \u6587\u4ef6 <code>fetch_users.php<\/code> \u5c06\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3001\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\uff0c\u5e76\u751f\u6210 XML \u683c\u5f0f\u7684\u6570\u636e\u8fd4\u56de\u7ed9\u524d\u7aef\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_xml_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\/\/ \u6267\u884c\u67e5\u8be2\n$sql = \"SELECT id, username, email FROM users\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u8bbe\u7f6e\u54cd\u5e94\u7c7b\u578b\u4e3a XML\nheader('Content-Type: application\/xml');\n\n\/\/ \u5f00\u59cb\u751f\u6210 XML \u6570\u636e\necho \"&lt;?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?&gt;\";\necho \"&lt;users&gt;\";\n\n\/\/ \u8f93\u51fa\u67e5\u8be2\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"&lt;user&gt;\";\n        echo \"&lt;id&gt;\" . $row&#91;\"id\"] . \"&lt;\/id&gt;\";\n        echo \"&lt;username&gt;\" . $row&#91;\"username\"] . \"&lt;\/username&gt;\";\n        echo \"&lt;email&gt;\" . $row&#91;\"email\"] . \"&lt;\/email&gt;\";\n        echo \"&lt;\/user&gt;\";\n    }\n} else {\n    echo \"&lt;message&gt;\u6ca1\u6709\u627e\u5230\u7528\u6237\u6570\u636e&lt;\/message&gt;\";\n}\n\necho \"&lt;\/users&gt;\";\n\n\/\/ \u5173\u95ed\u6570\u636e\u5e93\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>\u7528\u6237\u70b9\u51fb\u201c\u52a0\u8f7d\u7528\u6237\u201d\u6309\u94ae\uff0c\u89e6\u53d1 JavaScript \u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li>JavaScript \u4f7f\u7528 <code>XMLHttpRequest<\/code> \u5411 <code>fetch_users.php<\/code> \u53d1\u9001 GET \u8bf7\u6c42\u3002<\/li>\n\n\n\n<li><code>fetch_users.php<\/code> \u67e5\u8be2\u6570\u636e\u5e93\u5e76\u751f\u6210\u4e00\u4e2a\u5305\u542b\u7528\u6237\u4fe1\u606f\u7684 XML \u54cd\u5e94\u3002<\/li>\n\n\n\n<li>JavaScript \u901a\u8fc7 <code>xhr.responseXML<\/code> \u83b7\u53d6\u8fd4\u56de\u7684 XML \u6570\u636e\uff0c\u89e3\u6790 XML \u6570\u636e\u5e76\u663e\u793a\u7528\u6237\u5217\u8868\u3002<\/li>\n\n\n\n<li>\u9875\u9762\u4e0a\u7684 <code>#userList<\/code> \u5143\u7d20\u88ab\u66f4\u65b0\uff0c\u5c55\u793a\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u5230\u7684\u7528\u6237\u4fe1\u606f\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5. \u751f\u6210\u7684 XML \u683c\u5f0f\u54cd\u5e94<\/strong><\/h3>\n\n\n\n<p>PHP \u751f\u6210\u7684 XML \u6570\u636e\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;users&gt;\n    &lt;user&gt;\n        &lt;id&gt;1&lt;\/id&gt;\n        &lt;username&gt;Alice&lt;\/username&gt;\n        &lt;email&gt;alice@example.com&lt;\/email&gt;\n    &lt;\/user&gt;\n    &lt;user&gt;\n        &lt;id&gt;2&lt;\/id&gt;\n        &lt;username&gt;Bob&lt;\/username&gt;\n        &lt;email&gt;bob@example.com&lt;\/email&gt;\n    &lt;\/user&gt;\n    &lt;user&gt;\n        &lt;id&gt;3&lt;\/id&gt;\n        &lt;username&gt;Charlie&lt;\/username&gt;\n        &lt;email&gt;charlie@example.com&lt;\/email&gt;\n    &lt;\/user&gt;\n&lt;\/users&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. \u89e3\u6790 XML \u6570\u636e\u7684 JavaScript \u65b9\u6cd5<\/strong><\/h2>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0cJavaScript \u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u89e3\u6790 XML \u6570\u636e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>xhr.responseXML<\/code><\/strong>\uff1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u54cd\u5e94\u6570\u636e\u7684 XML \u6587\u6863\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong><code>getElementsByTagName()<\/code><\/strong>\uff1a\u901a\u8fc7\u6807\u7b7e\u540d\u83b7\u53d6\u6307\u5b9a\u8282\u70b9\u7684\u96c6\u5408\u3002<\/li>\n\n\n\n<li><strong><code>textContent<\/code><\/strong>\uff1a\u83b7\u53d6\u67d0\u4e2a\u8282\u70b9\u7684\u6587\u672c\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u7ed3\u5408\u4f7f\u7528 <strong>AJAX<\/strong>\u3001<strong>PHP<\/strong> \u548c <strong>XML<\/strong> \u53ef\u4ee5\u8ba9\u7f51\u9875\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u7684\u60c5\u51b5\u4e0b\u52a8\u6001\u52a0\u8f7d\u548c\u5c55\u793a\u6570\u636e\u3002\u5c3d\u7ba1\u73b0\u5728 JSON \u683c\u5f0f\u66f4\u4e3a\u5e38\u7528\uff0c\u4f46 XML \u4f9d\u7136\u662f\u4e00\u79cd\u53ef\u9760\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u7279\u522b\u662f\u5728\u67d0\u4e9b\u9057\u7559\u7cfb\u7edf\u4e2d\u4ecd\u7136\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7 AJAX \u5411 PHP \u53d1\u9001\u8bf7\u6c42\uff0cPHP \u67e5\u8be2 MySQL \u6570\u636e\u5e93\u5e76\u8fd4\u56de XML \u6570\u636e\uff0c\u524d\u7aef JavaScript \u89e3\u6790 XML \u6570\u636e\u5e76\u66f4\u65b0\u9875\u9762\u5185\u5bb9\uff0c\u8fd9\u79cd\u6a21\u5f0f\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u7528\u6237\u4f53\u9a8c\uff0c\u8fd8\u51cf\u5c11\u4e86\u9875\u9762\u5237\u65b0\u6b21\u6570\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AJAX \u548c XML\uff08\u53ef\u6269\u5c55\u6807\u8bb0\u8bed\u8a00\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6280\u672f\u7ec4\u5408\uff0c\u5728\u73b0\u4ee3 Web \u5f00\u53d1\u4e2d\u5e38\u5e38\u88ab\u7528\u6765\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u60c5\u51b5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3142","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\/3142","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=3142"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3142\/revisions"}],"predecessor-version":[{"id":3144,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3142\/revisions\/3144"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3143"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}