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.

Link to comment
Share on other sites

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;
  }
}

Link to comment
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.