Jump to content

Checkbox check all


treeleaf20

Recommended Posts

All,

I have the following checkbox code:

echo "<input name=\"picbigid[]\" type=\"checkbox\" value=\"$pic[src_big]\">";

 

I then want to select all the checkboxes so I have this code:

echo "<input type=\"button\" name=\"CheckAll\" value=\"Check All\" onClick=\"checkAll(document.photos.picbigid)\">  <input type=\"button\" name=\"UnCheckAll\" value=\"Uncheck All\" onClick=\"uncheckAll(document.photos.picbigid)\">";
[code=php:0]

My JS function is:
[code]
function checkAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = true ;
}
function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
field[i].checked = false ;
}

 

If I do it like this:

echo "<input name=\"picbigid\" type=\"checkbox\" value=\"$pic[src_big]\">";

It works but I can't have it like that because the form posts to another PHP page that needs the input name as an array.

 

Any ideas?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/183854-checkbox-check-all/
Share on other sites

You only need one function, just pass a true/false. Here is a working example -I'll let you convert it into the PHP code as needed

 

<html>
<head>
<script type="text/javascript">

function checkAll(formObj, groupName, checkState)
{
    var groupObj = formObj.elements[groupName];

    for (var i=0; i<groupObj.length; i++)
    {
        groupObj[i].checked = checkState;
    }
    return true;
}
</script>
</head>

<body>

<form>

  Box 1 <input name="picbigid[]" type="checkbox" value="value1" /><br>
  Box 2 <input name="picbigid[]" type="checkbox" value="value2" /><br>
  Box 3 <input name="picbigid[]" type="checkbox" value="value3" /><br>
  Box 4 <input name="picbigid[]" type="checkbox" value="value4" /><br>
   Box 5 <input name="picbigid[]" type="checkbox" value="value5" /><br>

  <input type="button" name="CheckAll" value="Check All"
    onClick="checkAll(this.form, 'picbigid[]', true);" />
    
  <input type="button" name="UnCheckAll" value="Uncheck All"
    onClick="checkAll(this.form, 'picbigid[]', false);" />

</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/183854-checkbox-check-all/#findComment-970790
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.