Using JSON Object with PHP
Creating JSON object in PHP
Define the key/value pairs to send the data to the client
Use json_encode() function to convert the object into JSON format before sending to the client
<?php
$myObj=(object)[];
$myObj->name = "Tanya Chauhan";
$myObj->age = 20;
$myObj->city = "Ghaziabad";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
To receive this data on the client side using AJAX and show on the web page using JavaScript built-in object JSON with methods
JSON.parse()
<!doctype html>
<html>
<head>
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
$("#b1").click(function()
{
$.post("jsontest.php",{},function(r,s)
{
var obj=JSON.parse(r);
var data="Name : "+obj.name+"<br>Age : "+obj.age+"<br>City : "+obj.city;
$("#result").html(data);
});
});
});
</script>
</head>
<body>
<input type="button" value="Show JSON Data" id="b1">
<div id="result"></div>
</body>
</html>
Sending JSON data from client to PHP page
Use JSON.stringify() function convert the JSON object into a string before sending to some PHP, Java, .NET etc.
Example
Input name, age and city from user and save into data using AJAX and PHP
File 1: savewithjson.php
<!doctype html>
<html>
<head>
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
$("#b1").click(function()
{
var name=$("#name").val();
var age=$("#age").val();
var city=$("#city").val();
var myObj = { "name":name, "age":age, "city":city };
var myJSON = JSON.stringify(myObj);
var url="savedata.php?data="+myJSON;
$.post(url,{},function(r,s)
{
alert(r);
});
});
});
</script>
</head>
<body>
<form>
<p>Name
<label for="name"></label>
<input type="text" name="name" id="name">
</p>
<p>Age
<label for="age"></label>
<input type="text" name="age" id="age">
</p>
<p>City
<label for="city"></label>
<input type="text" name="city" id="city">
</p>
<p>
<input type="button" id="b1" value="Submit">
</p>
</form>
</body>
</html>
File2: savedata.php
<?php
$data=$_GET['data'];
$cdata=json_decode($data);
echo "Name is ".$cdata->name.'<br>';
echo "Age is ".$cdata->age.'<br>';
echo "City is ".$cdata->city.'<br>';
?>
Creating JSON using PHP without JSON functions
<?php
$sql="Select * from student";
$cn=mysqli_connect("localhost","msme","password","msmeworld") or die("error");
$result=mysqli_query($cn,$sql);
$data="{\"students\": [";
while($r=mysqli_fetch_array($result))
{
$data=$data."{\"rollno\":\"$r[0]\",\"name\":\"$r[1]\",\"course\":\"$r[2]\",\"fees\":\"$r[3]\"},";
}
$data=substr($data,0,strlen($data)-1);
$data=$data."]}";
mysqli_close($cn);
echo $data;
?>
Define the key/value pairs to send the data to the client
Use json_encode() function to convert the object into JSON format before sending to the client
<?php
$myObj=(object)[];
$myObj->name = "Tanya Chauhan";
$myObj->age = 20;
$myObj->city = "Ghaziabad";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
To receive this data on the client side using AJAX and show on the web page using JavaScript built-in object JSON with methods
JSON.parse()
<!doctype html>
<html>
<head>
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
$("#b1").click(function()
{
$.post("jsontest.php",{},function(r,s)
{
var obj=JSON.parse(r);
var data="Name : "+obj.name+"<br>Age : "+obj.age+"<br>City : "+obj.city;
$("#result").html(data);
});
});
});
</script>
</head>
<body>
<input type="button" value="Show JSON Data" id="b1">
<div id="result"></div>
</body>
</html>
Sending JSON data from client to PHP page
Use JSON.stringify() function convert the JSON object into a string before sending to some PHP, Java, .NET etc.
Example
Input name, age and city from user and save into data using AJAX and PHP
File 1: savewithjson.php
<!doctype html>
<html>
<head>
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
$("#b1").click(function()
{
var name=$("#name").val();
var age=$("#age").val();
var city=$("#city").val();
var myObj = { "name":name, "age":age, "city":city };
var myJSON = JSON.stringify(myObj);
var url="savedata.php?data="+myJSON;
$.post(url,{},function(r,s)
{
alert(r);
});
});
});
</script>
</head>
<body>
<form>
<p>Name
<label for="name"></label>
<input type="text" name="name" id="name">
</p>
<p>Age
<label for="age"></label>
<input type="text" name="age" id="age">
</p>
<p>City
<label for="city"></label>
<input type="text" name="city" id="city">
</p>
<p>
<input type="button" id="b1" value="Submit">
</p>
</form>
</body>
</html>
File2: savedata.php
<?php
$data=$_GET['data'];
$cdata=json_decode($data);
echo "Name is ".$cdata->name.'<br>';
echo "Age is ".$cdata->age.'<br>';
echo "City is ".$cdata->city.'<br>';
?>
Creating JSON using PHP without JSON functions
<?php
$sql="Select * from student";
$cn=mysqli_connect("localhost","msme","password","msmeworld") or die("error");
$result=mysqli_query($cn,$sql);
$data="{\"students\": [";
while($r=mysqli_fetch_array($result))
{
$data=$data."{\"rollno\":\"$r[0]\",\"name\":\"$r[1]\",\"course\":\"$r[2]\",\"fees\":\"$r[3]\"},";
}
$data=substr($data,0,strlen($data)-1);
$data=$data."]}";
mysqli_close($cn);
echo $data;
?>
Comments
Post a Comment