JSON with PHP and Java Script

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.

Now let's give an example JSON array below to see the structure.

       {"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.

When creating JSON data, we need to observe the following rules.
  • 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"
Now let's learn to send arrays into JSON. In the following example we will look at how the directory is in JSON ...
       {"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.

First, let's look at the JSON functions in PHP.
  • 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 : 

With this function we can convert an array to JSON format. It is very simple to use. We created a JSON object now.

   $json = array('name' => 'Burak', 'surname' => 'Tufan', 'school' => 'KOU', 'study' => 'UEE');
   echo json_encode($json);


With this function, we can decode and understand an existing JSON array. So we read JSON data.

   $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.

Below the PHP code that created the JSON object:



if($_POST['id'] =="") {echo ""; return;} else {$id=($_POST['id']); }

	if($id!= "0")

		$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 );


In this way, we read the data posted on this page and output it in JSON form. Now let's see how to post in front and read the incoming data and finish our article ...

var values="id=" + stu_id;


            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


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.


  • There is no comment. Be the owner of first comment...