I have a list of users with a checkbox. Everytime a checkbox is checked, a function is called. The function gets the userid from the checkbox and this userid should be added into a php array and then into a cookie. If the cookie does not exist yet, it should be created. The value of the cookie should be the JavaScript parameter which is passed to this function (id).
My problem is that I don't know how to make this dynamically. E.g. when there is no userid yet in a cookie, it should be added to variable[0]. Furthermore, how can I pass the JavaScript id variable to PHP to set it as cookie value?
This is what I have tried so far:
function loadUserCalendar(id) {
console.log('user selected: ' + id);
// check if user is checked or not
if($('input[value="' + id+ '"]').is(':checked')) {
console.log('user ' + id + ' is checked now');
// add user to cookie
<?php
$calendar_users = $_COOKIE['calendar_users'];
if(empty($calendar_users)) {
session_start();
setcookie("calendar_users[" + $calendar_users.length + 1 + "]", "id", time()+3600*24*30*12);
session_commit();
?>
console.log('added user to cookie');
<?php
} else {
?>
console.log('going to add user to existing cookie');
<?php
}
?>
} else {
console.log('user ' + id + ' is UN-checked now');
// remove user from cookie
}
}
Any help is appreciated! Thank you.