PHPでのAjaxの使用方法は何ですか?

PHPでのAJAXの使用方法は以下の通りです:

  1. AJAXリクエストを含むJavaScript関数を作成する。例えば:
function loadData() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("result").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "ajax_data.php", true);
    xhttp.send();
}
  1. アジャックスデータ.php
<?php
// 连接到数据库 或 执行其他操作
// 查询数据
// 返回数据
echo "Data retrieved from server";
?>
  1. HTMLファイル内でJavaScript関数を呼び出し、結果を表示します。
<!DOCTYPE html>
<html>
<head>
    <title>AJAX Example</title>
    <script>
        function loadData() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("result").innerHTML = this.responseText;
                }
            };
            xhttp.open("GET", "ajax_data.php", true);
            xhttp.send();
        }
    </script>
</head>
<body>
    <button onclick="loadData()">Load Data</button>
    <div id="result"></div>
</body>
</html>

サーバーからデータを取得し、ページのコンテンツを更新するためにAJAXを使用できます。

コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds