php - Changing the value of a session with Ajax in Wordpress not working -
i´m kind of new using ajax trying update value of session using ajax. ajax call shoud fires when clicked on button.
when click on button returns succes function. using wordpress ajax call.
currently code:
ajax call:
$('.button').click(function(){ $.ajax({ type: "post", url: "/wp-admin/admin-ajax.php", data: {click: "true"}, success: function() { alert('bro worked!'); } }); });
functions.php in wordpress: session_start();
function notificationcall() { $_session['clicked'] = $_post['click']; die(); } add_action('wp_ajax_notificationcall', 'notificationcall'); add_action('wp_ajax_nopriv_notificationcall', 'notificationcall'); echo $_session['clicked'];
so ajax call returns succes function containing string "bro worked". however, session stays same default value of "false".
any ideas?
the reason it's true because ajax post data data: {click: "true"}
hardcoded true, , since that's you're setting value of $_session['clicked'] = $_post['click'];
, true.
one solution might hard code toggle in there:
function notificationcall() { if ($_session['clicked']) $_session['clicked'] = false; else $_session['clicked'] = true; die(); }
update
i think you'll need specify php function call inside ajax request data, should this:
data: {action: "notificationcall", click: "true"},
Comments
Post a Comment