Passing variables and data from PHP to Javascript

In this post I am showing you how to pass data or variables from PHP to Javascript. When we open a web page, HTML and Javascript loads in background, which can execute JS on that html.  It commonly occurs in your programming experience that you need to pass a variable from PHP to Javascript. Here I am discussing some ways, through which you can accomplish this task.

 1- Passing data directly to javascript:

This first method is very simple and easy to implement. Here is an example:

    var data = <?php echo json_encode("42"); ?>; //Don't forget the extra semicolon!

As you can see from the above example that this method is very easy to implement as the PHP variables are printed out directly to Javascript. Passing normal values would be easy but if you are passing data in the form of html or XML, you have to give special attention as it is not suitable for such a job.  The second point to keep in mind is, PHP does not have definite Javascript escape functions, so by using the method described above, your code may be open to second tier injections attack. You can JSON encode data, but as I said before, it may not work with complicated XML or HTML code, as by making ajax calls with such a practice asynchronously, can lead to major overhead.

2- Using AJAX technique:

Ajax is basically used to create interactive web applications. In the ajax method, your server side and client side scripts are completely separated, so you can take this process, somewhat secure. You dont have to mix PHP with Javascript and the code will be neat, clean and more readable. Debugging also becomes easy and less time consuming. You don’t have to wait for a page load to get the results of your request in ajax. But be aware that too much ajax calls produce network latencies because of so much http requests. In my opinion ajax is a better way to access php value in javascript.
ajax process flow
   Example with ajax using jQuery



   var name = jQuery('#name').val();

 // you can apply validation here on the name field."/ajax.php?name="+name , {

        }, function(response){





 HTML Code:

 <div id="results"></div>

  <input type="text" name="name" id="name" value=""> 

  <input type="button" class="button" id="savename" value="Save Name">

3- Printing the data into an html element and using Javascript to retrieve information from it.

I have used this method many times in my coding practice. It is not that much good as ajax, but still implementable. Its good point is, it still separates PHP and Javascript and works faster than ajax. The procedure to utilize this concept is, you create a hidden field with a specific name and id. That hidden field is used to store the data or information and then this data is fetched out of the hidden field. So, you can say that this element is only visible to Javascript, not the user or visitor. This method is not good, when playing with structured data as you have to escape and convert the strings first.


 <input type="hidden" id="data" name="data" value="<?php echo $val; ?>">

<!-- Or in Div based method  -->

<div id="data" style="display: none;">

    <?php    echo $val;       ?>




    var inner = document.getElementById("data");

    var val = div.innerContent;


Plz keep in mind that if you want to render html code, you have to use  <?php echo htmlspecialchars($val); ?> otherwise result will not be a valid HTML.

Add a Comment

Your email address will not be published. Required fields are marked *


My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib


Request a Quote

PHP Code Snippets Powered By :