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.

Method 1
Method 1 of 2:

Using "GET/POST" Commands

  1.  <!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.  <?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. 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
Method 2
Method 2 of 2:

Using Cookies

  1.  < 
     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.  <?php 
     echo 
     $_COOKIE 
     [ 
     "gfg" 
     ]; 
     ?> 
      
    
    • As coded, the cookies will expire within 10 days.
  3. Upload the new code to your website and visit it to see if the cookies are working.
    Advertisement

Community Q&A

Search
Add New Question
  • Question
    What happens when a cookie expires?
    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

      Advertisement

      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,929 times.

      Is this article up to date?

      Advertisement