Title: Pass JS Variable to PHP
Last modified: November 10, 2020

---

# Pass JS Variable to PHP

 *  [herng77](https://wordpress.org/support/users/herng77/)
 * (@herng77)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/pass-js-variable-to-php/)
 * I called a Hook from the order received page and want to collect the JS data 
   from user and pass it back to PHP to use the variable.
 * I tried to call with:
 *     ```
       <script>
             jQuery.ajax({
             type: "POST",
             url: window.location,
             data: { test:"test" },
             success: function( data ) {
               alert(data);
             }
           });
       </script>
       ```
   
 * and this to get
 * `echo $_POST['test'];`
 * but it is not working, the data is successfully posted but PHP cant read the 
   variables.
    Anybody know how to do it? Thanks for your help
    -  This topic was modified 5 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

Viewing 1 replies (of 1 total)

 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [5 years, 5 months ago](https://wordpress.org/support/topic/pass-js-variable-to-php/#post-13646712)
 * Have a read through this:
 * [https://codex.wordpress.org/AJAX_in_Plugins](https://codex.wordpress.org/AJAX_in_Plugins)
 * Basically, you need to set up an AJAX handler in your site that will be able 
   to work with that variable.

Viewing 1 replies (of 1 total)

The topic ‘Pass JS Variable to PHP’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * Last activity: [5 years, 5 months ago](https://wordpress.org/support/topic/pass-js-variable-to-php/#post-13646712)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
