• 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)
  • 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 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

    (@budster2k1)

    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.

    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.

    For another Javascript example, take a look at the article linked to in answer #1 of this StackOverflow thread, 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.

    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.