In my jquery script I have;
jQuery.ajax(
ajaxurl, {
action: 'lu_ajax',
status: 'enabled'
},
success: function (a) {
alert(a)
}
In my file.php i have;
add_action('wp_ajax_lu_ajax', 'lu_ajax');
add_action('wp_ajax_nopriv_lu_ajax', 'lu_ajax'); //for logged users
function lu_ajax() {
if($_GET['status'] != '') {
$value = $_GET['status'];
echo $value;
setcookie("status", $value, time()+3600, "/");
}
}
I am including the file.php in the main plugin such as require_once('inc/file.php');
However, no cookie is being set or any value being echoed, can anyone tell me what the issue is please. initially the issue that I had is that I was calling the file directly and caused wp functions to be undefined so i was recommended to use ajaxurl and then the action to perform and the variables i wanted to send over to php, but nothing is happening now.
ps. both jquery and php are in the same file.php i do not have a separate js file as it doesnt fit my purpose.
update new issue As I am making the ajax call from jquery i am firing them up based on conditionals, the following is a sample;
var stat= true;
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {action: 'lu_ajax', status: 'enabled'},
success: function (a) {
alert(a)
}
});
} else {
var stat= false;
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {action: 'lu_ajax', status: 'false'},
success: function (b) {
alert(b)
}
});
}
however the cookie is always set to enabled, meaning that the first call is only being made, the problem is not in the condition as i test it by setting it to false manually.