Jump to content

Having an All check box with multiple values passed into the array?


KitCarl

Recommended Posts

I have a check box group and would like to include a All check box, but do not now how to code the value to have the multiple values in one check box passed into the array?

 

<label>
    <strong>
    <input type="checkbox" name="association[]" value="1" checked id="association_4" />
    IBGA</strong></label>

  <strong>
  <label>
    <input type="checkbox" name="association[]" value="3" id="association_5" />
    PBGA</label>

  <label>
    <input type="checkbox" name="association[]" value="2" id="association_6" />
    NEHBA</label>

 

Here is the PHP code(My thanks to Cags for the implode statement). I would like to set the Else to be the All check box value.

 

IF (!empty($_POST['association']))			
	 	 $association = $association = "'" . implode("','", $_POST['association']) . "'";
	 		ELSE $association = "'1'";

 

Presently the check boxes names and values are hand coded, but they match names & values in a database table. I'd like to see the quick fix to learn how it is done, but assume the better way long term to handle it is by pulling the Names & Values from the database. While that is above my knowledge level, I wouldn't mind seeing how that is done to try to understand that method as well.

something like that

function checkUncheckAll() {
for(var i=0; i     if(inputs[i].type == 'checkbox') {
     // check the box or uncheck it.
         inputs[i].checked = inputs[i].checked ? false : true;
     }
}
}

Building upon Zanus's code, here is an example using your form elements (with some corrections to the HTML)

 

<html>
<head>

<style>
  label { font-weight: bold; }
</style>

<script type="text/javascript">

function checkUncheckAll(groupName, checkState)
{
    var groupObj = document.forms[0].elements[groupName];
    var groupLen = groupObj.length;
  
    for(var groupIdx=0; groupIdx<groupLen; groupIdx++)
    {
        groupObj[groupIdx].checked = checkState;
    }
    return;
}

</script>
</head>

<body>

<form name="test">

<input type="checkbox" name="association[]" value="1" checked id="association_4" />
<label for="association_4">IBGA</label>

<input type="checkbox" name="association[]" value="3" id="association_5" />
<label for="association_5">PBGA</label>

<input type="checkbox" name="association[]" value="2" id="association_6" />
<label for="association_6">NEHBA</label>

<br /><br />
<input type="checkbox" name="checkAll" value="x" id="checkAll" onclick="checkUncheckAll('association[]', this.checked)" />
<label for="checkAll">Check All</label>

</form>

</body>
</html>

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.