How to Pass Variables from Javascript to PHP

Download ArticleDownload Article

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.

Method 1
Method 1 of 2:

Using "GET/POST" Commands

Download Article
  1. How.com.vn English: Step 1 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]
  2. How.com.vn English: Step 2  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
  3. How.com.vn English: Step 3  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.
  4. Advertisement
Method 2
Method 2 of 2:

Using Cookies

Download Article
  1. How.com.vn English: Step 1  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>
  2. How.com.vn English: Step 2  Enter the following code for your server to use:
    <?php echo $_COOKIE["gfg"]; ?>
    • As coded, the cookies will expire within 10 days.
  3. How.com.vn English: Step 3  Test your code.
    Upload the new code to your website and visit it to see if the cookies are working.
  4. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What happens when a cookie expires?
    How.com.vn English: santosh kumar
    santosh kumar
    Community Answer
    When 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
      Thanks for submitting a tip for review!

      About This Article

      How.com.vn English: Darlene Antonelli, MA
      Written by:
      How.com.vn Technology Writer
      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.
      How helpful is this?
      Co-authors: 3
      Updated: January 6, 2021
      Views: 27,835
      Categories: Web Programming
      Article SummaryX

      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 27,835 times.

      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.

      Notices:
      • - 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.

      Advertisement