Jump to content

Check All function and Checkbox array conflict


dasein

Recommended Posts

I have a function in the header which simply allows for all the checkboxes of the same name to be selected:

 

function checkAll(field)

{

for (i = 0; i < field.length; i++)

field.checked = true ;

}

 

function uncheckAll(field)

{

for (i = 0; i < field.length; i++)

field.checked = false ;

}

 

But I can't use the [] brackets like this with that function.

 

<form name="test" action="testform.php" method="post">

<input type='checkbox' name='member_type[]' value='FM'>Full Member<br>

<input type='checkbox' name='member_type[]' value='JM'>Junior Member<br>

<input type='checkbox' name='member_type[]' value='LM'>Lady Member<br>

<input type='checkbox' name='member_type[]' value='MM'>Make Member<br>

<input type='checkbox' name='member_type[]' value='OM'>Outside Member<br>

<input type="submit" value="SUBMIT">

 

To run a php script to read the checkboxes as an array (there are actually about 50 with the same name), I have to have the [] brackets.

 

$types = $_POST['member_type']; // array of values selected by the user

 

foreach($types as $key=>$val){

    $types_search.=$val.", ";

}

 

I want to have both, but they seem to not work together. Is there a minor workaround to this? I hope I explained this okay. Thank you.

you can utilize javascript's "node" functions

 

for example

 

if you added in an id for those forms

 

lets say you id'd one 'test'

 

you could do

function checkAll(eid) {
  var children = document.getElementById(eid).childNodes;
  for (i = 0; i < children.length; i++) {
    children[i].checked = true;
  }
}

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.