JSON with PHP and Java Script
Hello everyone in this article we will talk about JSON and how to use it with Javascript and PHP. How can we send and receive JSON data between PHP and Javascript.What is JSON ?
- Stands for Java Script Object Notation.
- Very Fast It is a data transport system.
- It is not dependent on any language.
- It is easy to understand, create and write.
JSON is a form of an array, called JavaScript, which indicates that it was prepared with that logic. JSON is a system available in all languages.
{
"classes":
[
{"class":"Java", "teacher":"Burak"},
{"class":"CSharp", "teacher":"Hamdi"},
{"class":"SQL", "teacher":"Tufan"}
]
}
I have created a sample JSON array as we can see in this example ...
JSON Data is generated with Java Script Logic.
- JSON data name - Consists of value pairs
- Created name - value pairs separated by commas
- Ornamental brackets hold this name - value pairs
- The square brackets hold the fancy brackets.
Now let's examine them in order
We can think of it as a kind of property - value. When creating, " " must be enclosed in quotation marks."name" : "Burak"An example is a JSON name value pair. Now let's create a sample JSON array using them.
{
"name" : "Burak Hamdi",
"surname" : "TUFAN"
}
{
"classes":
[
{"class":"Java", "teacher":"Burak"},
{"class":"CSharp", "teacher":"Hamdi"},
{"class":"SQL", "teacher":"Tufan"}
]
}
As we have seen above, we keep the arrays with angular warrants and keep them in a json array again.
We kept the course names and the names of their teachers under the series of courses. We kept the classes inside a JSON array as if it were a JSON object.
We can read JSON data with Javascript, while JSON.parse can read it.
Usage JSON with PHP
Now let's see how to use JSON data with PHP.
Firstly, after PHP 5.2, JSON data can be read directly in PHP. We need to know that in lower versions will not work.
- json_encode: Write a prepared array in JSON format
- json_last_error: Checks if there is an error when executing a JSON function
- json_decode: Decodes an existing JSON array and makes it understandable to PHP.
JSON_ENCODE method :
<?php
$json = array('name' => 'Burak', 'surname' => 'Tufan', 'school' => 'KOU', 'study' => 'UEE');
echo json_encode($json);
?>
JSON_DECODE method:
<?php
$json = array('name' => 'Burak', 'surname' => 'Tufan', 'school' => 'KOU', 'study' => 'UEE');
$json_out = (json_decode($json));
$json_out = (json_decode($json, true));
?>
Above we can decode and read our JSON object ... Now let's see how we can communicate between each other.
Let's have a sample PHP file and get JSON data from another php page using AJAX and print it
first create JSON data on the back page with PHP, then call the data from the front with AJAX, then parse it and print it on the screen.
<?php
$id="";
if($_POST['id'] =="") {echo ""; return;} else {$id=($_POST['id']); }
if($id!= "0")
{
include('sql_connection.php');
$query = mysql_query("select * from students where id='$id'");
while ($res= mysql_fetch_array($query )) {
$output = array('school_number' => $res["no"] ,'name' => $res["name"], 'surname' => $res["surname"], 'class' => $res["class"]);}
echo json_encode($output );
}
}
?>
var values="id=" + stu_id;
$.ajax({
type: "post",
url: "student_detail.php",
data : values,
success : function(answer){
var json = answer;
var obj = JSON.parse(json);
document.getElementById('name').innerHTML = obj.name;
document.getElementById('surname').innerHTML = obj.surname;
document.getElementById('class').innerHTML = obj.class;
document.getElementById('number').innerHTML = obj.number;
}
});
As you can see, we have moved the JSON object between Ajax and PHP Script using Ajax and printed our data on the front page.
That's all in this article.
Keep following me.
Have good coding.
Burak Hamdi TUFAN
Comments