[SOLVED] - Select/Unselect all checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

[SOLVED] - Select/Unselect all checkboxes

Post by Wade »

Hi every one, I've searched the site but haven't been able to find an answer to my issue.

I'm creating a dynamic table of data with a check box on the leftside. What I want to do is be able to select all or unselect all with a master check box. I can get this to work with javascript if the checkbox name is static eg. record instead of and array record[].

Does anyone have any suggestions?

Javascript:

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!--

<!-- Begin
function Check(chk)
{
if(document.myform.Check_ctr.checked==true){
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}else{

for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
}

// End -->
</script>
PHP:

Code: Select all

...
<?php session_start();
if(isset($_SESSION['myusername'])){?>
<form name="myform" method="post" action="med.php" >
<th><input type="checkbox" name="Check_ctr" value="yes" onClick="Check(document.myform.record)"></th>
<?php } ?>

...

echo("<td><input type=\"checkbox\" name=\"record\" value=" . $row['ContactID'] . "></td>");
...
The above works for selecting all, but won't pass all the data in the array...
Below passes all the data, but no master select option...

Code: Select all

...
<?php session_start();
if(isset($_SESSION['myusername'])){?>
<form name="myform" method="post" action="med.php" >
<th><input type="checkbox" name="Check_ctr" value="yes" onClick="Check(document.myform.record[])"></th>
<?php } ?>

...

echo("<td><input type=\"checkbox\" name=\"record[]\" value=" . $row['ContactID'] . "></td>");
...

Thanks in advance.
Last edited by Wade on Wed May 23, 2007 12:16 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

document.myform.record[] is not a specifier.

document.forms['myform'].elements['record[]'] is.
Wade
Forum Commoner
Posts: 41
Joined: Mon Dec 18, 2006 10:21 am
Location: Calgary, AB, Canada

Post by Wade »

feyd wrote:document.myform.record[] is not a specifier.

document.forms['myform'].elements['record[]'] is.
Awesome, works like a charm.

Thank you very much!
Post Reply