I am trying to get all checkboxes checked with:
<script type="text/javascript">
$(document).ready(function()
{
$("#delmsg_all").click(function()
{
var checked_status = this.checked;
$("input[(name=delmsg[])]").each(function()
{
this.checked = checked_status;
});
});
});
</script>
All checkboxes get checked.
form part:
<input type="checkbox" name="delmsg[]" value="<?php echo $aRows['pvID'];?>"/>
So far, so good, but…..
i expect an array with all checked values, but i dont get an array back?
code for checking what is sent by my form:
## Berichten verwijderen
// test op eigen berichten??
if(isset($_POST['todo']) AND $_POST['todo'] == 'verwijder_mijn_berichten')
{
if(is_array($_POST['delmsg']))
$aMeldingen[] = 'yessss';// shows text in jquery lightbox
else
$aErrors[] = 'test?'; // just used as testvalue
foreach($_POST['delmsg'] AS $key => $value)
{
$aMeldingen[] = $key.' => '.$value;
echo $key.' => '.$value;
}
}
Problem:
when i use delmsg as name of the checkboxes, the jquery function works, all checkboxes become checked, but after submitting the form, no array is recognized.
when i use delmsg[] as name of my checkboxes, the jquery function does NOT check all the checkboxes, but the array is recognized well. (after i selected some checkboxes by hand)
Where does it go wrong with my thoughts?
Thanks in advance for any help!
It’s a little bit problematic to use brackets in name attribute. jQuery selector engine sizzle uses it for a different purpose, so it does not recognize it directly. You have to escape it twice like
\\[\\]. (From jQuery documentation) You can get your checkboxes like this:$("input[name=delmsg\\[\\]]")If it’s not important you can give same class to your checkboxes and then select them with class name;
HTML/PHP
Javascript
By the way, if you will not do something else in
eachiteration, you can set checked property with just one command;.prop()If you don’t want to use extra class, you may get checkboxes with
:checkboxselector.