Page 1 of 1

[SOLVED] - Select/Unselect all checkboxes

Posted: Wed May 23, 2007 11:45 am
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.

Posted: Wed May 23, 2007 12:13 pm
by feyd
document.myform.record[] is not a specifier.

document.forms['myform'].elements['record[]'] is.

Posted: Wed May 23, 2007 12:16 pm
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!