Download Article
Download Article
In many instances, JavaScript is used on the client-side and PHP is used on the server-side of a website. This wikiHow will teach you how to pass variables (or data) between JavaScript and PHP using either a "GET/POST" method or using cookies.
Steps
-
Enter the following code into your HTML:
<!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 >
- This code lets the user to your website enter information. [1] X Research source
-
Enter the following code into your PHP code on your server:
<?php $result = $_GET [ 'data' ]; echo $result ; ?>
- Even though the user entered information in a JavaScript environment, their data will be passed through to PHP on the server-side.
Advertisement -
Test your code. Upload the new code to your website, generally using an FTP. After it's uploaded, enter test data to see if your code works.
Advertisement
-
Enter the following code into your website coding:
< 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 >
-
Enter the following code for your server to use:
<?php echo $_COOKIE [ "gfg" ]; ?>
- As coded, the cookies will expire within 10 days.
-
Test your code. Upload the new code to your website and visit it to see if the cookies are working.
Advertisement
Community Q&A
Search
-
QuestionWhat happens when a cookie expires?santosh kumarCommunity AnswerWhen a cookie expires it becomes invalid and is no longer sent to the server by the browser. The user's browser automatically removes it from the browser's cookie storage. This means that the cookie is no longer accessible or retrievable by the server or client-side scripts. When the user's browser makes subsequent requests to the server, it no longer includes the expired cookie in the request headers. The server does not receive any information related to the expired cookie, and server-side scripts cannot access or utilize its data.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Tips
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
About This Article
Article Summary
X
1. Enter the following code into your HTML.
2. Enter the following code into your PHP code on your server.
3. Test your code.
Did this summary help you?
Thanks to all authors for creating a page that has been read 29,466 times.
Advertisement