Việc sử dụng JSON phổ biến là đọc dữ liệu từ một máy chủ web và hiển thị dữ liệu trong một trang web.
Chương này sẽ dạy bạn cách trao đổi dữ liệu JSON giữa máy khách và máy chủ PHP.
Các tập tin PHP
PHP có một số chức năng tích hợp để xử lý JSON.
Các đối tượng trong PHP có thể được chuyển đổi thành JSON bằng cách sử dụng hàm PHP json_encode []:json_encode[]:
Tệp PHP
Tên = "John"; $ myobj-> tuổi = 30; $ myobj-> city = "New York";
$myObj->name = "John";
$myObj->age = 30;
$myObj->city
= "New York";
$ myjson = json_encode [$ myobj];
echo $ myjson;?>
?>
Hiển thị tệp PHP »
Máy khách JavaScript
Dưới đây là JavaScript trên máy khách, sử dụng cuộc gọi AJAX để yêu cầu tệp PHP từ ví dụ trên:
Thí dụ
Sử dụng json.parse [] để chuyển đổi kết quả thành đối tượng JavaScript:
const xmlHttp = new xmlhttprequest []; xmlhttp.onload = function [] {& nbsp; & nbsp; const myObj = json.parse [this.responsetext]; & nbsp; & nbsp; document.getEuityById ["demo"]. innerHtml = myObj.Name; } xmlhttp.open ["get", "demo_file.php"]; xmlhttp.send [];
xmlhttp.onload =
function[] {
const myObj = JSON.parse[this.responseText];
document.getElementById["demo"].innerHTML = myObj.name;
}
xmlhttp.open["GET", "demo_file.php"];
xmlhttp.send[];
Hãy tự mình thử »
Mảng PHP
Các mảng trong PHP cũng sẽ được chuyển đổi thành JSON khi sử dụng hàm PHP json_encode []:json_encode[]:
Tệp PHP
Tên = "John"; $ myobj-> tuổi = 30; $ myobj-> city = "New York";
$myArr = array["John", "Mary", "Peter", "Sally"];
$ myjson = json_encode [$ myobj];
echo $ myjson;?>
?>
Hiển thị tệp PHP »
Máy khách JavaScript
Dưới đây là JavaScript trên máy khách, sử dụng cuộc gọi AJAX để yêu cầu tệp PHP từ ví dụ trên:
Thí dụ
Sử dụng json.parse [] để chuyển đổi kết quả thành đối tượng JavaScript:
const xmlHttp = new xmlhttprequest []; xmlhttp.onload = function [] {& nbsp; & nbsp; const myObj = json.parse [this.responsetext]; & nbsp; & nbsp; document.getEuityById ["demo"]. innerHtml = myObj.Name; } xmlhttp.open ["get", "demo_file.php"]; xmlhttp.send [];
xmlhttp.onload = function[] {
const myObj = JSON.parse[this.responseText];
document.getElementById["demo"].innerHTML = myObj[2];
}
xmlhttp.open["GET", "demo_file_array.php", true];
xmlhttp.send[];
Hãy tự mình thử »
Mảng PHP
Các mảng trong PHP cũng sẽ được chuyển đổi thành JSON khi sử dụng hàm PHP json_encode []:
Imagine you have a database on your server, and you want to send a request to it from the client where you ask for the 10 first rows in a table called "customers".
$ myjson = json_encode [$ myarr];
Dưới đây là JavaScript trên máy khách, sử dụng cuộc gọi AJAX để yêu cầu tệp PHP từ ví dụ mảng ở trên:
Thí dụ
Sử dụng json.parse [] để chuyển đổi kết quả thành đối tượng JavaScript:
const xmlHttp = new xmlhttprequest []; xmlhttp.onload = function [] {& nbsp; & nbsp; const myObj = json.parse [this.responsetext]; & nbsp; & nbsp; document.getEuityById ["demo"]. innerHtml = myObj.Name; } xmlhttp.open ["get", "demo_file.php"]; xmlhttp.send [];
const dbParam = JSON.stringify[limit];
xmlhttp = new XMLHttpRequest[];
xmlhttp.onload = function[] {
document.getElementById["demo"].innerHTML = this.responseText;
}
xmlhttp.open["GET","json_demo_db.php?x=" + dbParam];
xmlhttp.send[];
Hãy tự mình thử »
Mảng PHP
- Các mảng trong PHP cũng sẽ được chuyển đổi thành JSON khi sử dụng hàm PHP json_encode []:
- Convert the object into a JSON string.
- $ myjson = json_encode [$ myarr];
- Dưới đây là JavaScript trên máy khách, sử dụng cuộc gọi AJAX để yêu cầu tệp PHP từ ví dụ mảng ở trên:
- Sử dụng json.parse [] để chuyển đổi kết quả thành mảng javascript:
var xmlHttp = new xmlhttprequest []; xmlhttp.onload = function [] {& nbsp; & nbsp; const myObj = json.parse [this.responsetext]; & nbsp; & nbsp; document.getEuityById ["demo"]. innerHtml = myObj [2]; } xmlhttp.open ["get", "demo_file_array.php", true]; xmlhttp.send [];
Tệp PHP
Tên = "John"; $ myobj-> tuổi = 30; $ myobj-> city = "New York";
header["Content-Type: application/json; charset=UTF-8"];
$obj = json_decode[$_GET["x"], false];
$ myjson = json_encode [$ myobj];
$stmt = $conn->prepare["SELECT name FROM customers LIMIT ?"];
$stmt->bind_param["s", $obj->limit];
$stmt->execute[];
$result = $stmt->get_result[];
$outp = $result->fetch_all[MYSQLI_ASSOC];
echo $ myjson;?>
?>
Hiển thị tệp PHP »
- Máy khách JavaScriptjson_decode[].
- Dưới đây là JavaScript trên máy khách, sử dụng cuộc gọi AJAX để yêu cầu tệp PHP từ ví dụ trên:
- Thí dụjson_encode[] function.
Sử dụng json.parse [] để chuyển đổi kết quả thành đối tượng JavaScript:
Thí dụ
Sử dụng json.parse [] để chuyển đổi kết quả thành đối tượng JavaScript:
const myObj = JSON.parse[this.responseText];
let text = "";
for [let x in myObj] {
text += myObj[x].name + "
";
}
document.getElementById["demo"].innerHTML
= text;
}
Hãy tự mình thử »
Mảng PHP
Các mảng trong PHP cũng sẽ được chuyển đổi thành JSON khi sử dụng hàm PHP json_encode []:
To send AJAX requests using the POST
method, specify the method, and the correct header.
$ myjson = json_encode [$ myarr];
Thí dụ
Sử dụng json.parse [] để chuyển đổi kết quả thành đối tượng JavaScript:
const xmlhttp = new XMLHttpRequest[];
xmlhttp.onload = function[] {
const myObj = JSON.parse[this.responseText];
let text ="";
for [let x in myObj] {
text += myObj[x].name + "
";
}
document.getElementById["demo"].innerHTML = text;
}
xmlhttp.open["POST", "json_demo_db_post.php"];
xmlhttp.setRequestHeader["Content-type", "application/x-www-form-urlencoded"];
xmlhttp.send["x=" + dbParam];
Hãy tự mình thử »
Mảng PHP
Tệp PHP
Các mảng trong PHP cũng sẽ được chuyển đổi thành JSON khi sử dụng hàm PHP json_encode []:
?>