0

This might be a scope issue but I want to cover all bases. I am successfully sending a value, distance, to an Ajax handler function. Here is the JS:

    $.ajax({
    type: 'POST',
    url: url,
    data: {
        'action':'example_ajax_request',
        'distance' : distance
    },
    success:function(response) {
        // This outputs the result of the ajax request
        console.log(response);
    },
    error: function(errorThrown){
        console.log(errorThrown);
    }
});  

I have access to the distance variable in the backend:

add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );

add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

// Dynamic field population

function example_ajax_request() {

    // The $_REQUEST contains all the data sent via ajax 
    if ( isset($_POST) ) {

        $distance = $_POST['distance'];
        // echo $distance;

    }

   die();
}

$distance contains the value and I can echo it successfully. But I need to use this value in another function that hooks into gravity forms. I need to dynamically set a gravity forms value (using their gform_field_value_$parameter_name hook). Problem is that I cannot figure out a way to access the $_POST['distance'] value outside of the Ajax function and the Gravity Forms hook fires before the $_POST['distance'] is available (my guess).

Is there a way to access the Ajax sent $_POST object outside of the handler function in WP? Am I looking at this all wrong? Any suggestions on how I can use that value to dynamically populate a Gravity Forms field?

Thanks in advance.

UPDATE:

I'm trying to set the GF value in the backend if possible, not using the Ajax response (using jQuery)

  • Are you trying to set the value in the form on page load or after some action client side? What action is triggering the ajax? Form submission? – jdm2112 Aug 9 '16 at 14:55
  • After the submit button is clicked. I'm using Google Places API (JS) to get the distance between 2 points. I'm then trying to dynamically populate a GF field with that distance. So the ajax call is triggered on submit of the Google form – Tony M Aug 9 '16 at 15:03
2

As I understand your question, you want to Update a GF field with the results ($distance) of an AJAX request.

If that is correct, on a successful AJAX request, you just need to update the value of your GF field.

Assuming your response is the actual value correctly formatted. here's what you need to do.

$.ajax({
  type: 'POST',
  url: url,
  data: {
    'action':'example_ajax_request',
    'distance' : distance
  },
  success:function(response) {
    // This outputs the result of the ajax request
    console.log(response);
    $('#your-GF-ID').val( response ); // This is where you update your GF field 
  },
  error: function(errorThrown){
    console.log(errorThrown);
  }
}); 
  • Thanks bynicolas. That is what I'm planning on doing as a final resort. But I'm hesitant to set the value on the client side because I don't want it to be changed. – Tony M Aug 9 '16 at 16:26
  • Well why are you worried about that? in what context are you using this value? Are you trying to save this value? If you are using AJAX then there is no way for you to hook into a function since the page is already loaded. If you want to achieve that, you will have to not use AJAX, save your value to the DB and then use that saved value into your GF field. If you don't save the value, there is always the risk of that value being changed or lost – bynicolas Aug 9 '16 at 16:51
  • The value will be used to determine price at some point (when gf is submitted). I don't need to save it. Looks like I've been looking at it wrong (with the page being loaded and all) and Ajax is not my solution unless I'm ok with that value being possible to edit. I appreciate the insight. Thanks. – Tony M Aug 9 '16 at 17:01
  • If you are only worried that the user accidently plays with that value, you could always set your ACF field to be read only to discourage this kind of behaviour. I assume that you will be using this in a sort of "estimate shipping" price, then I believe this approach is appropriate. If you need to work with real or validated info, then you will need to record those info in the user profile or another table of that sort. – bynicolas Aug 9 '16 at 17:08
  • *facepalm I didn't even think of that. So if I set the GF field to readonly I can still populate it with the Ajax response? I'm about to test that right now. And yes, I don't want anyone "accidentally" modifying the value. Thanks a bunch – Tony M Aug 9 '16 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.