When a client want to send data to the server , convert the data in the form of text (JSON) and send to the server. The default method of sending data to the server is GET.
By using method stringify
var jsonObj = JSON.stringify(jObj);
After converting to JSON text, send to the jsonreceive.php page by appending it to the variable x.
("jsonreceive.php?x=" + jsonObj).
When we receive JSON text from the client in the page jsonreceive.php ,the value will be in the form of PHP string. A PHP built in function json_encode() will return a JSON text.
var myObj = JSON.parse(myJSONa);
Program to receive JSON Text
<?php $myjson=$_GET['x']; ?> <!DOCTYPE html> <html> <body> <h1> When we receive JSON text from the client in the page jsonreceive.php ,the value will be in the form of PHP string. A PHP built in function json_encode() will return a JSON text. </h1> <p id="p1"></p> <script> var myjsonObj = <?php echo json_encode($myjson); ?>; var myObj = JSON.parse(myjsonObj); document.getElementById("p1").innerHTML = myObj.country; </script> </body> </html>
Keep both files jsonsend.php, jsonreceive.php in the root directory of PHP (www folder). Details about root directory , please visit this link : https://codeunplug.com/how-to-install-php-in-ubuntu/ .
JSON methods are listed in https://www.tutorialspoint.com/json/json_overview.htm .
First load the jsonsend.php in the browser (localhost/jsonsend.php), it will redirect the data in the jsonsend.php is to the jsonreceive.php page and you can see the result as shown below.