When I click on checkbox (either check it or uncheck it) I want to send its status(on/off) and value of id (or name) to php ( then to sql).
My pseudocode is as follows:
$("input:checkbox").click(function () {
var cat_id = jQuery(this).attr('name');
var status = [];
if($("input:checkbox").is(':checked')) {
return $(this).attr();
} else {
return $(this).attr();
}
$.post("var_dump.php", {
cat_id: status
}, // should be id(name of chbox) and its value 1-on, 0-off
function (response) {
$("#result").text(response); //just for checking
});
return true;
});
I will have many rows with data and check-box for each row (so unchecked box will not count ), I can use .map() or .each() to build an array but I want new values every time I change status of check-box – so every time it should be new $.post value.
Reason for array is that I want to make buttons to un-check five or ten (last) check-boxes/rows so it should be one time array. (but this will be implemented later.)
In your code the return statement will not even wait for post code to execute. Remove those unwanted return statements and try this. Also define
statusas a object instead of array since you want to send a key/value pair in the post request.