Title: ajax return javascript to php
Last modified: August 20, 2016

---

# ajax return javascript to php

 *  [budster2k1](https://wordpress.org/support/users/budster2k1/)
 * (@budster2k1)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/ajax-return-javascript-to-php/)
 * I have added some ajax that posts a javascript variable that I need to be able
   to use in a php function I have been able to see the output in the browser inspector
   as a preview but I cant get it to echo on screen. How do I get this to a php 
   variable I can use in a function to determine users local time based on their
   timezone.
 *     ```
       jQuery.ajax({
       	type: 'post',
       	url: '/wp-admin/admin-ajax.php',
       	data:{
       		'action':'get_tz',
       		'tz':timezone.name()
       	},
       	dataType: 'html',
       	success:function(data){
   
           },
       	error: function(errorThrown){
       		alert('error');
       		console.log(errorThrown);
       	}
       });
       ```
   
 *     ```
       wp_localize_script( 'daily_tee_tz', 'get-tz', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
   
       add_action( 'wp_ajax_get_tz', 'get_tz_handler' );
       add_action( 'wp_ajax_nopriv_get_tz', 'get_tz_handler' );
       function get_tz_handler() {
       echo $_POST['tz'];
       die(); // Make sure you do this!
       }
       ```
   

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

 *  [pdclark](https://wordpress.org/support/users/pdclark/)
 * (@pdclark)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/ajax-return-javascript-to-php/#post-2997515)
 * Your current code is correct, and the value for `$_POST['tz']` is being correctly
   passed to PHP because the response is displaying in the web inspector.
 * Your problem is not with WordPress, but with jQuery: once you echo `$_POST['tz']`,
   that is available to jQuery for use, but you have to tell jQuery how to display
   that information somewhere on your page.
 * **Example 1**
    Alert your response from the ajax call.
 *     ```
       jQuery.ajax({
           type: 'post',
           url: '/wp-admin/admin-ajax.php',
           data:{
               'action':'get_tz',
               'tz':timezone.name()
           },
           dataType: 'html',
           success:function(data){
               alert(data);
           },
           error: function(errorThrown){
               alert('error');
               console.log(errorThrown);
           }
       });
       ```
   
 * **Example 2**
    Load the response into an element with ID “response”.
 *     ```
       jQuery.ajax({
           type: 'post',
           url: '/wp-admin/admin-ajax.php',
           data:{
               'action':'get_tz',
               'tz':timezone.name()
           },
           dataType: 'html',
           success:function(data){
               jQuery('#response').html( data );
           },
           error: function(errorThrown){
               alert('error');
               console.log(errorThrown);
           }
       });
       ```
   
 * **Example 3**
    Use [jQuery.load](http://api.jquery.com/load/) instead of jQuery.
   ajax() for simpler loading into an HTML element with ID response.
 *     ```
       jQuery('#response').load( '/wp-admin/admin-ajax.php', {
           'action':'get_tz',
           'tz':timezone.name()
       });
       ```
   
 *  Thread Starter [budster2k1](https://wordpress.org/support/users/budster2k1/)
 * (@budster2k1)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/ajax-return-javascript-to-php/#post-2997555)
 * Thanks for the advise I can get your examples to work but what I think I am looking
   for is a way to set a cookie then use php to read that cookie I got this to work
   with
 *     ```
       jQuery.ajax({
       	type: 'post',
       	url: '/wp-admin/admin-ajax.php',
       	data:{
       		'action':'get_tz',
       		'tz':timezone.name()
       	},
       	dataType: 'html',
       	success:function(data){
       		document.cookie = "timezone=" + data;
           },
       	error: function(errorThrown){
       		alert('error');
       		console.log(errorThrown);
       	}
       });
       ```
   
 * but what happens on the first load is the cookie is set and it shows on a reload
   is there a way to set this cookie before the page loads. The variable I am looking
   to set as a cookie is timezone by name. Then is php I will be able to do a time
   comparison and display content accordingly. I was also wondering if ajax is nessecary
   to set this cookie value. Thanks for any help.
 *  [pdclark](https://wordpress.org/support/users/pdclark/)
 * (@pdclark)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/ajax-return-javascript-to-php/#post-2997557)
 * It is not possible to set a cookie via javascript before a page loads, but you
   can follow my original examples and use jQuery to modify your page content without
   reloading the page.
 * AJAX is not neccissary to set a cookie, but it is required for sending information
   about your client’s timezone from Javascript without reloading. You can achieve
   client timezone detection with no javascript or AJAX (before loading) by using
   a PHP Geolocation library, such as [geoPlugin](http://www.geoplugin.com/webservices/php).
 * For another Javascript example, take a look at the article linked to in answer#
   1 of [this StackOverflow thread](http://stackoverflow.com/questions/1905397/how-to-get-clients-timezone),
   as well as the full example given in answer #2.
 * This question isn’t really related to WordPress in any way at this point. You
   may be best served in jQuery-related forums.
 *  [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * (@benracicot)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/ajax-return-javascript-to-php/#post-2997656)
 * I have the same TYPE of problem where I have JS variables that I need to send
   along with my AJAX
 *     ```
       data = {
       	action:'wpa56343_search',
       	latitude: $('input#latitude').attr('value'),
       	longitude: $('input#longitude').attr('value')
        };
           	_do_ajax(data);
       }); // end init func
   
           	function _do_ajax(obj) {
           	console.log(obj);
               $.ajax({
                   type:"POST",
                   url: My_Obj.ajaxurl,
                   data: data,
                   dataType: 'json',
                   success: function( response ){ loop with query data }
       ```
   
 * This work, but I cannot get $_POST[] variables out of it on the PHP side to include
   in my query…

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

The topic ‘ajax return javascript to php’ is closed to new replies.

 * 4 replies
 * 3 participants
 * Last reply from: [BenRacicot](https://wordpress.org/support/users/benracicot/)
 * Last activity: [12 years, 10 months ago](https://wordpress.org/support/topic/ajax-return-javascript-to-php/#post-2997656)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
