Is it possible to add values to SESSION w/ jQuery
I have a form with many rows of items, each line with checkbox that has item's ID.
I need to take item ID and add it to php $_SESSION when a single checkbox or all checkboxes selected (REMOVE FROM SESSION IF UNCHECKED). Currently I have the following code to select all checkboxes:
var checkboxes = $(".chkbx").change(function() {开发者_运维知识库
var allIsChecked = checkboxes.length === checkboxes.filter(":checked").length;
all[0].checked = allIsChecked;
enableMyElement(checkboxes.filter(":checked").length > 0);
putInSession($(this).val());
});
var all = $("#checkall").change(function() {
checkboxes.attr("checked",this.checked);
enableMyElement(this.checked);
});
function putInSession(id) {
$.ajax({
url: actions.php,
type: "POST",
data: "action=addToSession&ID="+id;
});
}
<input id="checkall" type="checkbox">
<input class="chkbx" type="checkbox" value="360" name="delCont[]">
<input class="chkbx" type="checkbox" value="500" name="delCont[]">
<input class="chkbx" type="checkbox" value="510" name="delCont[]">
How do I update my PHP session with jQuery? Is it even possible? What are the alternatives?
UPDATE:
OK, Ajax is clearly the way to go. Added new function putInSession(). Still not clear how to integrate with my SelectAll fnctionality...
You can use ajax to call a script to add the item id to the session
Like others have stated use jQuery ajax. Have a look here for a tutorial.
Call a PHP script through ajax:
$.ajax({
url: 'script.php',
data: {
session_var : 'bla_bla_bla'
}
});
That should do the trick.
精彩评论