Jump to content

WordPress AJAX. jQuery Post data not available


BenRacicot

Recommended Posts

Hello everyone!

I've been at this WordPress AJAX functionality for almost 2 weeks and I've had it working in a basic state for about 2 days. However passing along geolocation data WITH the $.ajax data is ruining my life. Here's what's going on:

 

// click "trigger" function
$("#searchsubmit").click(function(e){
     e.preventDefault();

  // data: object
  data = {
     action:'wpa56343_search',
     latitude: $('input#latitude').attr('value'),
     longitude: $('input#longitude').attr('value')
  };
    _do_ajax(data);
}); // end click func
        
    function _do_ajax(obj) {
     console.log(obj);
        $.ajax({
            type:"POST",
            url: My_Obj.ajaxurl, // the request is sent to admin-ajax.php
            data: data,
            dataType: 'json',
            success: function( response ){ loops and stuff }

WordPress is very specific as to the ways you can do AJAX requests. If you're not familiar with them: The above queries a wp_ajax_ACTION in functions.php. My action function is called wpa56343_search. This function is where I need to include post variables for use within the query it performs.

 

The way it is working now logs 

Object {action: "wpa56343_search", latitude: "42.041261299999995", longitude: "-70.9414206"} 

Now why can't I get these through traditional methods such as:

$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
// to use within the query function
function my_ajax_search($latitude, $longitude){ query here }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.