Javascript | JSON PHP

PHP

JSON stands for JScript banks North-W ONotation ▪ Table. Used to exchange and store data from a web server. JSON uses JavaScript object notation. JavaScript objects can be converted to JSON and receive JSON formatted text into JavaScript objects. Converting a JavaScript object to JSON is done using this function:
JSON.stringify (object) 
Converting JSON to JavaScript is done using this function:
 JSON.parse (string_format) 
Data exchange from the server, PHP is used as the server language. The JSON.parse() function is used to fetch data from php or any other server. To get data from the server, multiple AJAX operators check if the server is ready to respond to data from the server or not. If these conditions are met, then you can get data from a php file. The protocols used to send and receive data from the server are defined as follows:
XMLHttpRequest() 
PHP files and its client-side JavaScript:consider php- a file in which a person's personal data is given, such as name, gender, age. The data from the php object must be JSON encoded. This file is saved by engineer.php < tbody> $myObj = new stdClass(); $myObj -> name = "Geeks" ; $myObj -> college = "NIT" ; $myObj -> gender = "Male" ; $myObj -> age = 30; $myJSON = json_encode ( $myObj ); echo $myJSON ; ?> From a php file, data is sent to JSON via an "echo" and the data will be passed to the client's JavaScript. In php file json_encode() is used to convert objects in php file to json format. To access data from a php file through client-side JavaScript, use the following script: < html > < body > < h1 style = "color: # 090; text-align: center;" > GeeksforGeeks < / h1 > < p style = "font-size: 25px" > JSON get data from a PHP file on the server. < / p > < h4 > Author Name: < / h4 > < p id = "name" > < / p > < h4 > College: < / h4 > < p id = "college" > < / p > < h4 > Gender: < / h4 > < p id = "gender" > < / p > < h4 > Age: < / h4 > < p id = "age" > < / p > < script >  
var xmlhttp = new XMLHttpRequest();  
xmlhttp.onreadystatechange = function() { if (this.readyState == 4 & amp; & amp; this.status == 200) { myObj = JSON.parse (this.responseText); document.getElementById ("name"). innerHTML = myObj.name; document.getElementById ("college"). innerHTML = myObj.college; document.getElementById ("gender"). innerHTML = myObj.gender; document.getElementById ("age"). innerHTML = myObj.age; } };
xmlhttp.open ("GET", "engineer.php", true);
xmlhttp.send(); < / script > < / body > < / html >
Output: In the script JSON.parse (this.responseText)the function is used to parse data into a variable so that it can call values ​​from that object. This this.responseis used to get data from php which is printed as a string. In this code, php object data is retrieved using JSON script. Using the AJAX function in the script, the data is checked to see if it is responding, when it responded, the data will be sent and printed on the web page. The xmlhttp.open ("GET", "engineer.php", true)function is used to get a value from the engineer.phpphp file. The xmlhttp.send()function is used to send values ​​to XMLHttpRequest() .PHP Array and its client-side JavaScript:Consider a php file that consists of an array of name. The data is encoded in JSON and will be printed using "echo". $arrDay = array (

"Monday" , "Tuesday" ,

"Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday"); $arrJSON = json_encode ( $arrDay ); echo $arrJSON ; ?> Allows you to access data from a php file array using the client JavaScript. To do this, consider the following HTML file. < html > < body > < h1 style = "color: # 090; text-align: center;" > GeeksforGeeks < / h1 > < p style = "font-size: 25px" > JSON get data from PHP file, and converting into JavaScript array. < / p > < p style = " font-size: 25px; " > Second Date of week: < / p > < p id = "day" > < / p > < script > var xmlhttp = new XMLHttpRequest();  
xmlhttp.onreadystatechange = function() { if (this.readyState == 4 & amp; & amp; this.status == 200) { var myObj = JSON.parse (this.responseText); document.getElementById ("day"). innerHTML = myObj [2]; } };
xmlhttp.open ("GET", "daylist.php", true);
xmlhttp.send(); < / script > < / body > < / html >
Output:
The variable is parsed in the array in JavaScript, this value is called by the js array that was initialized in the above line. PHP Database:retrieving data from the database is the same as retrieving data from a regular php file. But a few additional queries will be added. In this process, first the data from the database must be extracted into a php file, and then this data must be taken by JavaScript to project it onto the client server. First, create a MySQL database using php. The following program is used to create a table and insert some data into it. $conn = new mysqli ( "localhost" , " root " , " " , "engineerforengineer" ); $createTable = " create table engineer (names varchar (255)) " ; $tableadd = $conn -> query ( $createTable ); $conn -> query ( " insert into engineer values ​​('Geeks')" ); $conn -> query ( "insert into engineer values ​​(' gfg') " ); $conn -> query ( "insert into engineer values ​​(' g4g') " ); $conn -> close(); ?> Now fetch data from another php script. $conn = new mysqli ( "localhost" , "root" , "" , "engineerforengineer" ); $result = $conn -> query ( "select names from engineer" ); $output = array (); $output = $result -> fetch_all (MYSQLI_ASSOC) ; $sresult = json_encode ( $output ); echo $sresult ; $conn -> close(); ?> Now let's collect some data in client-side JavaScript and print the result.

< tbody> < html > < body > < h1 style = "color: # 090; text-align: center;" > GeeksforGeeks < / h1 > < p style = "font-size: 25px" > JSON received the data from the PHP file < / p > < p id = "arrayContent" > < / p > < script > var obj, xmlhttp; xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 & amp; & amp; this.status == 200) { document.getElementById ("arrayContent"). innerHTML = this.responseText; } }; xmlhttp.open ("GET", "data.php", true); xmlhttp.send(); < / script > < / body > < / html > This is then sent to the client and the result is printed in a JSON structure, since these are not filtered values ​​in every other value.
output:
PHP loop :is a combination of an array and the two aforementioned topics (PHP Array and its JavaScript and PHP client database) with their integration with loops in it. This JavaScript code parses the values ​​into a variable, these values ​​are called by arrays, and after each myObj is printed, its values ​​will increment to the next value.

< html > < body > < h1 style = "color: # 090; text-align: center;" > GeeksforGeeks < / h1 > < p style = "font-size: 25px" > JSON received the data from the PHP file < / p > < p id = "arrayContent" > < / p > < script > var obj, xmlhttp, myObj, x, txt = ""; xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 & amp; & amp; this.status == 200) { myObj = JSON.parse (this.responseText); for (x in myObj) { txt + = myObj [x] .names + "< br > "; } document.getElementById (" arrayContent "). innerHTML = txt; } }; xmlhttp.open ("GET", "data.php", true); xmlhttp.send(); < / script > < / body > < / html > Output: PHP = POST method: everythingeasy if all of the above topics are fully understood ... The following syntax will change. In the POST method, arguments must be passed through the send method, but in the GET method, arguments can be passed when the php file request is sent.GET method:
open (" GET ", file_name? X = argument, asyn, username, password) 
POST method:
open (" POST ", file_name, asyn, username , password) 
Arguments are passed by the send (arguments)method and send a request from php to access data from the server. So use the function to receive the request:
setRequestHeader ("Content-type", "application / x-www-form-urlencoded") 
The header must be added in the php file:
header ("Content-Type: application / json; charset = UTF-8") 
,The below php file can be saved with post.php header ( "Content-Type: application / json; charset = UTF-8" ); $obj = json_decode ( $_ POST [ "x" ], false); $conn = new mysqli ( "localhost" , "root" , "" , "engineerforengineer" ); $result = $conn -> query ( "select names from" . $obj -> table); $output = array (); $output = $result -> fetch_all (MYSQLI_ASSOC) ; $sresult = json_encode ( $output ); code class = "plain"> header ( "Content-Type: application / json; charset = UTF-8" ); $obj = json_decode ( $_ POST [ "x" ], false); $conn = new mysqli ( "localhost" , "root" , "" , "engineerforengineer" );



Get Solution for free from DataCamp guru