.ﺖﺳﺍ ﺭﻭﺮﺳ ﺏﻭ ﮏﯾ ﺯﺍ ﺎﻫ ﻩﺩﺍﺩ ﻥﺪﻧﺍﻮﺧ JSON ﺞﯾﺍﺭ ﯼﺎﻫﺩﺮﺑﺭﺎﮐ ﺯﺍ ﯽﮑﯾ
ﺪﯿﻨﮐ ﻪﻟﺩﺎﺒﻣ ﺮﮕﯾﺪﮑﯾ ﻦﯿﺑ ﺍﺭ JSON ﯼﺎﻫ ﻩﺩﺍﺩ ﻪﻧﻮﮕﭼ ﻪﮐ ﺪﻫﺩ ﯽﻣ ﺩﺎﯾ ﺎﻤﺷ ﻪﺑ ﻞﺼﻓ ﻦﯾﺍ
PHP ﺖﯾﺮﯾﺪﻣ ﯼﺍﺮﺑ ﯽﻠﺧﺍﺩ ﻊﺑﺍﻮﺗ ﯽﺧﺮﺑ ﯼﺍﺭﺍﺩ JSON ﺖﺳﺍ.
ﺩﺮﮐ ﻞﯾﺪﺒﺗ JSON ﻪﺑ PHP ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ PHP ﺭﺩ ءﺎﯿﺷﺍ
<?php
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New
York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ PHP ﺖﺳﺍﻮﺧﺭﺩ ﯼﺍﺮﺑ AJAX ﯽﻧﺍﻮﺧﺍﺮﻓ ﮏﯾ ﺯﺍ ﻪﮐ ﺖﺳﺍ ﺖﻨﯾﻼﮐ ﯼﻭﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﮏﯾ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﮏﯾ ﻪﺑ ﻪﺠﯿﺘﻧ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ()JSON.parse ﺯﺍ
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
xmlhttp.open("GET", "demo_file.php");
xmlhttp.send();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Get JSON Data from a PHP Server</h2>
<p id="demo"></p>
<script>
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
xmlhttp.open("GET", "demo_file.php");
xmlhttp.send();
</script>
</body>
</html>
ﺪﻧﻮﺷ ﯽﻣ ﻞﯾﺪﺒﺗ JSON ﻪﺑ ﺰﯿﻧ PHP ﺭﺩ ﺎﻫ ﻪﯾﺍﺭﺁ ،PHP ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻡﺎﮕﻨﻫ
<?php
$myArr = array("John", "Mary", "Peter", "Sally");
$myJSON = json_encode($myArr);
echo $myJSON;
?>
ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ PHP ﺖﺳﺍﻮﺧﺭﺩ ﯼﺍﺮﺑ AJAX ﯽﻧﺍﻮﺧﺍﺮﻓ ﮏﯾ ﺯﺍ ﻪﮐ ﺖﺳﺍ ﺖﻨﯾﻼﮐ ﯼﻭﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﮏﯾ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﯾﺍﺭﺁ ﻪﺑ ﻪﺠﯿﺘﻧ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ()JSON.parse ﺯﺍ
var xmlhttp = new XMLHttpRequest();
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();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Get JSON Data from a PHP Server</h2>
<p>Convert the data into a JavaScript array:</p>
<p id="demo"></p>
<script>
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj[2];
}
xmlhttp.open("GET", "demo_file_array.php");
xmlhttp.send();
</script>
</body>
</html>
PHP ﺩﺮﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﯼﺍﺮﺑ ﻥﺁ ﺯﺍ ﻥﺍﻮﺗ ﯽﻣ ﻭ ﺖﺳﺍ ﺭﻭﺮﺳ ﺖﻤﺳ ﯽﺴﯾﻮﻧ ﻪﻣﺎﻧﺮﺑ
ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ ﻥﺁ ﻪﺑ ﺍﺭ ﯽﺘﺳﺍﻮﺧﺭﺩ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﻭ ﺪﯾﺭﺍﺩ ﺩﻮﺧ ﺭﻭﺮﺳ ﯼﻭﺭ ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ ﮏﯾ ﺪﯿﻨﮐ ﺭﻮﺼﺗ
.ﺪﻨﮐ ﻒﯿﺻﻮﺗ ،ﺪﯿﻧﺍﺩﺮﮔﺮﺑ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﻪﮐ ﺍﺭ ﯽﯾﺎﻫ ﻒﯾﺩﺭ ﺩﺍﺪﻌﺗ ﻪﮐ ﺪﯾﺯﺎﺴﺑ JSON ﯽﺷ ﮏﯾ ،ﺖﻨﯾﻼﮐ ﺭﺩ
ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ a ﻪﺑ ﺍﺭ JSON ﯽﺷ ،ﺭﻭﺮﺳ ﻪﺑ ﺖﺳﺍﻮﺧﺭﺩ ﻝﺎﺳﺭﺍ ﺯﺍ ﻞﺒﻗ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ JSON ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺷ ﻞﯾﺪﺒﺗ ﯼﺍﺮﺑ()JSON.stringify ﺯﺍ
const limit = {"limit":10};
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();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Get JSON Data from a PHP Server</h2>
<p>The JSON received from the PHP file:</p>
<p id="demo"></p>
<script>
const dbParam = JSON.stringify({"limit":10});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xmlhttp.open("GET", "json_demo_db.php?x=" + dbParam);
xmlhttp.send();
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﻒﯾﺮﻌﺗ ﺍﺭ "Limit" ﺭﺍﺪﻘﻣ ﻭ ﯽﮔﮋﯾﻭ ﮏﯾ ﯼﻭﺎﺣ ﯽﺷ ﮏﯾ
.ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ JSON ﻪﺘﺷﺭ ﮏﯾ ﻪﺑ ﺍﺭ ﯽﺷ
.ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ ﺮﺘﻣﺍﺭﺎﭘ ﻥﺍﻮﻨﻋ ﻪﺑ JSON ﻪﺘﺷﺭ ﺎﺑ PHP ﻞﯾﺎﻓ ﻪﺑ ﺖﺳﺍﻮﺧﺭﺩ ﮏﯾ
(JSON ﻥﺍﻮﻨﻋ ﻪﺑ) ﺩﺩﺮﮔﺮﺑ ﻪﺠﯿﺘﻧ ﺎﺑ ﺖﺳﺍﻮﺧﺭﺩ ﺎﺗ ﺪﯿﻨﮐ ﺮﺒﺻ
.PHP ﻞﯾﺎﻓ ﺯﺍ ﯽﺘﻓﺎﯾﺭﺩ ﻪﺠﯿﺘﻧ ﺶﯾﺎﻤﻧ
:ﺪﯾﺯﺍﺪﻨﯿﺑ ﯽﻫﺎﮕﻧ PHP ﻞﯾﺎﻓ ﻪﺑ
<?php
header("Content-Type: application/json; charset=UTF-8");
$obj =
json_decode($_GET["x"], false);
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$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 json_encode($outp);
?>
ﺪﯿﻨﮐ ﻞﯾﺪﺒﺗ ﯽﺷ ﮏﯾ ﻪﺑ ﺍﺭ ﺖﺳﺍﻮﺧﺭﺩ PHP ﻊﺑﺎﺗ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ
.ﺪﯿﻨﮐ ﺮﭘ ﯽﺘﺳﺍﻮﺧﺭﺩ ﯼﺎﻫ ﻩﺩﺍﺩ ﺎﺑ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ ﻭ ﺪﯿﻨﮐ ﺍﺪﯿﭘ ﯽﺳﺮﺘﺳﺩ ﻩﺩﺍﺩ ﻩﺎﮕﯾﺎﭘ ﻪﺑ
ﺪﯿﻧﺍﺩﺮﮔﺮﺑ JSON ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺍﺭ ﯽﺷ ﻭ ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﯽﺷ ﮏﯾ ﻪﺑ ﺍﺭ ﻪﯾﺍﺭﺁ
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
let text = "";
for (let x in myObj) {
text += myObj[x].name + "<br>";
}
document.getElementById("demo").innerHTML = text;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Get JSON Data from a PHP Server</h2>
<p id="demo"></p>
<script>
const obj = { "limit":10 };
const dbParam = JSON.stringify(obj);
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
myObj = JSON.parse(this.responseText);
let text = ""
for (let x in myObj) {
text += myObj[x].name + "<br>";
}
document.getElementById("demo").innerHTML = text;
};
xmlhttp.open("GET", "json_demo_db.php?x=" + dbParam);
xmlhttp.send();
</script>
<p>Try changing the "limit" property from 10 to 5.</p>
</body>
</html>
.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ code class="w3-codespan">POST HTTP> ﺵﻭﺭ ﺯﺍ ﺖﺳﺍ ﺮﺘﻬﺑ ﺐﻠﻏﺍ ،ﺭﻭﺮﺳ ﻪ
.ﺪﯿﻨﮐ ﺺﺨﺸﻣ ﺍﺭ ﺢﯿﺤﺻ ﺭﺪﻫ ﻭ ﺵﻭﺭ ،POST
ﺵﻭﺭ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ
:ﺪﺷﺎﺑ send()
ﺵﻭﺭ ﻥﺎﻣﻮﮔﺭﺁ ﺪﯾﺎﺑ ﻥﻮﻨﮐﺍ ﺭﻭﺮﺳ ﻪﺑ ﻩﺪﺷ ﻝﺎﺳﺭ
const dbParam = JSON.stringify({"limit":10});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
const myObj = JSON.parse(this.responseText);
let text ="";
for (let x in myObj) {
text += myObj[x].name + "<br>";
}
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);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Use HTTP POST to Get JSON Data from a PHP Server</h2>
<p id="demo"></p>
<script>
const dbParam = JSON.stringify({"limit":10});
const xmlhttp = new XMLHttpRequest();
xmlhttp.onload = function() {
myObj = JSON.parse(this.responseText);
let text = "";
for (let x in myObj) {
text += myObj[x].name + "<br>";
}
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);
</script>
<p>Try changing the "limit" property from 10 to 5.</p>
</body>
</html>
.ﺖﺳﺍ ﻪﺘﻓﺎﯾ ﻝﺎﻘﺘﻧﺍ ﯼﺎﻫ ﻩﺩﺍﺩ ﺖﻓﺎﯾﺭﺩ ﺵﻭﺭ PHP ﻞﯾﺎﻓ ﺭﺩ ﺕﻭﺎﻔﺗ ﺎﻬﻨﺗ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ POST_$ﺯﺍ GET_$ﯼﺎﺟ ﻪﺑ
<?php
header("Content-Type: application/json; charset=UTF-8");
$obj =
json_decode($_POST["x"], false);
$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");
$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 json_encode($outp);
?>