Title: Passing jquery variable to PHP
Last modified: August 31, 2016

---

# Passing jquery variable to PHP

 *  [jetfighter](https://wordpress.org/support/users/jetfighter/)
 * (@jetfighter)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/passing-jquery-variable-to-php/)
 * Hello, I have a list of dropdown options for a survey, and am counting their 
   choices using jquery. The counting code works fine and has been confirmed. The
   trouble comes with passing the variable to PHP (from what I’ve read, I’ll need
   to use the POST function but am having trouble) in order to modify the user’s
   meta data based on the survey responses.
 * Here’s the jquery / counting code **which works fine**:
 *     ```
       $('select').change(function() {
           // get all selects
           var eSelects = $('select.e');
   
           // set values count by type
           var eyes = 0;
   
           // for each select increase count
           $.each(eSelects, function(i, s) {
               // increase count
               if($(s).val() == '1') { eyes++; }
           });
   
           // update count values summary
           $('.cnteyes').text(eyes);
       });
       ```
   
 * And here’s the PHP which is not working (don’t understand how to use the POST
   function, so left that out):
 *     ```
       <?php
       $response = 'cnteyes';
       if ( ! add_user_meta( get_current_user_id(), 'survey', $response, true ) ) {
          update_user_meta ( get_current_user_id(), 'survey', $response );
       }
   
       echo get_user_meta( get_current_user_id(), 'survey', true );
       ?>
       ```
   
 * Any help would be greatly appreciated! I’m completely stuck and do not understand
   how to pass jquery to PHP. Thanks for your time.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Pablo Alexis](https://wordpress.org/support/users/pelao777/)
 * (@pelao777)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/passing-jquery-variable-to-php/#post-7127662)
 * try with ajax… var js must be passed by ajax to php
 *  Thread Starter [jetfighter](https://wordpress.org/support/users/jetfighter/)
 * (@jetfighter)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/passing-jquery-variable-to-php/#post-7127796)
 * Thanks Pablo. So for the URL portion of ajax, I’d use: [http://www.website.com/wp-admin/admin-ajax.php](http://www.website.com/wp-admin/admin-ajax.php)?
 *  [Evan Herman](https://wordpress.org/support/users/eherman24/)
 * (@eherman24)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/passing-jquery-variable-to-php/#post-7127799)
 * You’ll need to re-write what you have, as it’s not going to support an AJAX call
   without first setting up the PHP handler.
 * [http://stackoverflow.com/questions/10589018/how-to-add-ajax-to-wordpress-theme](http://stackoverflow.com/questions/10589018/how-to-add-ajax-to-wordpress-theme)
 * This article will also be of some help, even tho it is listed as ‘ajax in plugins’
   it should also work with themes:
    [https://codex.wordpress.org/AJAX_in_Plugins](https://codex.wordpress.org/AJAX_in_Plugins)
 * Sample AJAX call:
 *     ```
       jQuery(document).ready(function($) {
   
       		var data = {
       			'action': 'my_action',
       			'whatever': 1234
       		};
   
       		// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
       		jQuery.post(ajaxurl, data, function(response) {
       			alert('Got this from the server: ' + response);
       		});
       	});
       ```
   
 * Which would send an ajax request to a function hooked into `wp_ajax_my_action`(
   for logged in users) and `wp_ajax_nopriv_my_action` for non-logged in users.
 * On a successfull request, you’ll get an alert back from the server with the text‘
   Got this from the server: some text you send back’.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Passing jquery variable to PHP’ is closed to new replies.

## Tags

 * [jquery](https://wordpress.org/support/topic-tag/jquery/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [Evan Herman](https://wordpress.org/support/users/eherman24/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/passing-jquery-variable-to-php/#post-7127799)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
