How to pass values from JavaScript to PHP

Warning message

JQuery Cycle must be installed in order to run the slideshow. Please go to Status Report for instructions.

We cannot assign JavaScript variables to PHP directly . To do this AJAX comes into play. You can pass data and variables asynchronously . 

To do this you have to pass HTTP request asynchronously using Jquery Ajax method http://api.jquery.com/jQuery.ajax/ .

To do this first include the Jquery library in your file at the top so that you can use this ajax method . Current version of jquery is 1.10 

you can use this line : <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Then you have to include the code for sending the variable from javascript to PHP.

$.ajax({
  type: "POST",
  url: "save_data.php",
  data: "title="+strTitle+"&name_email="+name_email+"&result="+result,
   cache: false,
})

In above you have defined POST method , so you can receive the values in save_data.php using $_POST variable easily like $_POST['title'] .

In URL save_data.php is the file where you are sending the javascript variables to php, to use or save the data in the database.

Data string can be made by that or like this :

data: { name: "John", location: "Boston" }

In the end set cache to false, so that it cannot cache the url.

and thats it!

Now you can receive values in your php file and can do the operations with your data.