JavaScript is the client side and PHP is the server side script language. The way to pass a JavaScript variable to PHP is through a request.
Method 1: This example uses form element and GET/POST method to pass JavaScript variables to PHP. The form of contents can be accessed through the GET and POST actions in PHP. When the form is submitted, the client sends the form data in the form of a URL such as:
https://example.com?name=value
This type of URL is only visible if we use the GET action, the POST action hides the information in the URL.
Client Side:
<!DOCTYPE html> <html> <head> <title> Passing JavaScript variables to PHP </title> </head> <body> <h1 style="color:green;"> GeeksforGeeks </h1> <form method="get" name="form" action="destination.php"> <input type="text" placeholder="Enter Data" name="data"> <input type="submit" value="Submit"> </form> </body> </html> |

Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result.
<?php $result = $_GET['data']; echo $result; ?> |
Output:

Method 2: Using Cookies to store information:
Client Side: Use Cookie to store the information, which is then requested in the PHP page. A cookie named gfg is created in the code below and the value GeeksforGeeks is stored. While creating a cookie, an expire time should also be specified, which is 10 days for this case.
<script> // Creating a cookie after the document is ready $(document).ready(function () { createCookie("gfg", "GeeksforGeeks", "10"); }); // Function to create the cookie function createCookie(name, value, days) { var expires; if (days) { var date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = "; expires=" + date.toGMTString(); } else { expires = ""; } document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/"; } </script> |
Server Side(PHP): On the server side, we request for the cookie by specifying the name gfg and extract the data to display it on the screen.
<?php echo $_COOKIE["gfg"]; ?> |
Output:

Recommended Posts:
- How to pass variables and data from PHP to JavaScript ?
- How to pass PHP Variables by reference ?
- How to pass form variables from one page to other page in PHP ?
- How to pass a PHP array to a JavaScript function?
- JavaScript | Pass string parameter in onClick function
- How to pass an array as a function parameter in JavaScript ?
- How to Pass Image as a parameter in JavaScript function ?
- JavaScript Course | Variables in JavaScript
- How to pass integer by reference in Java
- Perl | Pass By Reference
- How to pass string to a component using Angular2 ?
- How to pass two parameters to EventEmitter in Angular 9 ?
- How to pass query parameters with a routerLink ?
- Variables and Datatypes in JavaScript
- How to unset JavaScript variables?
- Global and Local variables in JavaScript
- How to unpack array elements into separate variables using JavaScript ?
- What are undeclared and undefined variables in JavaScript?
- View the list of all variables in Google Chrome Console using JavaScript
- How to declare variables in different ways in 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.


