I made a basic ajax request to 2 variables. I want the values to be sent and manipulated in the back-end in order to use them in a database query. All that should happen on page load. My main.js code:
main.js file
var lat = 21.458965;
var lng = -11.336985;
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: {'action':'func',
'lat':lat,
'lng':lng},
success:function(data){
// note that i don't want the data to return back to the front-end.
},
error:function(errorThrown){
console.log(errorThrown);
}
});
}
functions.php
wp_register_script( 'main', plugin_dir_url( __FILE__ ).'js/main.js', array( 'jquery' ) );
wp_enqueue_script( 'main' );
function func(){
if(isset($_REQUEST)){
//i tried to retrieve using $_POST and $_GET but the 2 arrays where empty.
// i need to retrieve both vars and take them out of the function then use them in a db query.
}
}
add_action('wp_ajax_func', 'func');
add_action('wp_ajax_nopriv_func', 'func');
What i wanted here is to send data (2 int variables to be specific) from javascript file to functions.php so any other way to do that please tell me about it.
To do that i created a form with 2 hidden input elements using JS then submit it with $('form').submit(); but the page was loading twice(the normal page load and the submit reload).