How to pass variables and data from PHP to JavaScript ?
In this article, let’s see how to pass data and variables from PHP to Javascript.
We can pass data from PHP to JavaScript in two ways depending on the situation. First, we can pass the data using the simple assignment operator if we want to perform the operation on the same page. Else we can pass data from PHP to JavaScript using Cookies. Cookie work in client-side.
Program 1: This program passes the variables and data from PHP to JavaScript using assignment operator.
<!DOCTYPE html> <html> <head> <title> How to pass variables and data from PHP to JavaScript? </title> </head> <body style="text-align:center;"> <h1 style="color:green;">GeeksforGeeks</h1> <h4> How to pass variables and data from PHP to JavaScript? </h4> <?php $name = "Hello World"; ?> <script type="text/javascript"> var x = "<?php echo"$name"?>"; document.write(x); </script> </body> <html> |
Output:

Here, we just take input by statically or dynamically and pass it to JavaScript variable using the assignment operator. The PHP code in the JavaScript block will convert it into the resulting output and then pass it to the variable x and then the value of x is printed.
Program 2: This program passes the variables and data from PHP to JavaScript using Cookies.
<!DOCTYPE html> <html> <head> <title> How to pass variables and data from PHP to JavaScript? </title> </head> <body style="text-align:center;"> <h1 style="color:green;">GeeksforGeeks</h1> <h4> How to pass variables and data from PHP to JavaScript? </h4> <?php // Initialize cookie name $cookie_name = "user"; $cookie_value = "raj"; // Set cookie setcookie($cookie_name, $cookie_value); if(!isset($_COOKIES[$cookie_name])) { print("Cookie created | "); } ?> <script type="text/javascript"> var x = document.cookie; document.write(x); </script> </body> <html> |
Output:

PHP provides a method to set the cookie using the setCookie() method. Here, we can set the data or variable in PHP cookie and retrieve it from JavaScript using document.cookie.
Recommended Posts:
- How to pass JavaScript variables to PHP ?
- How to pass PHP Variables by reference ?
- How to pass a PHP array to a JavaScript function?
- GRE Data Analysis | Distribution of Data, Random Variables, and Probability Distributions
- JavaScript | Pass string parameter in onClick function
- How to unset JavaScript variables?
- What are undeclared and undefined variables in JavaScript?
- Global and Local variables in JavaScript
- How to unpack array elements into separate variables using JavaScript ?
- View the list of all variables in Google Chrome Console using JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Data Types in JavaScript
- How to get form data using JavaScript/jQuery?
- How to append data to <div> element using JavaScript ?
- How to get the data attributes of an element using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



