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.

Link to comment
Share on other sites

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>

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.