Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.