This article was co-authored by How.com.vn staff writer, Darlene Antonelli, MA. Darlene Antonelli is a Technology Writer and Editor for How.com.vn. Darlene has experience teaching college courses, writing technology-related articles, and working hands-on in the technology field. She earned an MA in Writing from Rowan University in 2012 and wrote her thesis on online communities and the personalities curated in such communities.
This article has been viewed 27,835 times.
Learn more...
In many instances, JavaScript is used on the client-side and PHP is used on the server-side of a website. This How.com.vn 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]
- 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.
- 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.
Community Q&A
- 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.
Tips
About This Article
1. Enter the following code into your HTML.
2. Enter the following code into your PHP code on your server.
3. Test your code.
Is this article up to date?
⚠️ Disclaimer:
Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.
- - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
- - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
- - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
- - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.