wordpress - Using data sent via AJAX in multiple functions on a WP plugin -


this might scope issue want cover bases. sending value, distance, ajax handler function. here js:

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

i have access distance variable in 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() {      // $_request contains data sent via ajax      if ( isset($_post) ) {          $distance = $_post['distance'];         // echo $distance;      }     die(); } 

$distance contains value , can echo successfully. need use value in function hooks gravity forms. need dynamically set gravity forms value (using gform_field_value_$parameter_name hook). problem cannot figure out way access $_post['distance'] value outside of ajax function , gravity forms hook fires before $_post['distance'] available (my guess).

is there way access ajax sent $_post object outside of handler function in wp? looking @ wrong? suggestions on how can use value dynamically populate gravity forms field?

thanks in advance.

here found out:

there no way use ajax data in hook since wp loaded. data available in front end. end, would:

    $.ajax({   type: 'post',   url: url,   data: {     'action':'example_ajax_request',     'distance' : distance   },   success:function(response) {     // outputs result of ajax request     console.log(response);     $('#your-gf-id').val( response ); // update gf field    },   error: function(errorthrown){     console.log(errorthrown);   } });  

the other option saving data in db , foregoing ajax.


Comments