Jump to content

[SOLVED] function for multiple checkbox groups on one page


Recommended Posts

Hello everyone,

 

I am trying to write a function that takes an array of options and presents them to the user. The catch is that I need to reuse it several times on one page to create multiple unique checkbox groups.

 

I know that one usually makes the name attribute of each input tag match the name of the array of items that is being presented to the user, like this:

<input type="checkbox" id="color1" name="colors[]" value="white" />

 

But I need to be able to set a unique name for each group of checkboxes. I've written some code to try to create a dynamically named array, but it isn't working.

 

Does anyone know what's wrong with my code, or if there is a better way of doing this?

<?php

if(isset($_POST['submit']))
{
print_r(array_keys($_POST));
}

$colors = array('red','blue','green','yellow','purple','black','white','orange','pink');

function listOptions($options, $groupName)
{
//create dynamically named variable
$$options = $groupName;

$checkBoxName = $groupName . '[]';
$count = 0;

foreach($options as $option)
{
	$id = $groupName . $count;
	echo "\t<p><input type=\"checkbox\" id=\"$id\" name=\"$checkBoxName\" value=\"$option\" /><label for=\"$id\">$option</label></p>\n";
	$count++;
}
}
?>

<h1>Favorite colors:</h1>
<form name="test_form" method="post">
<?php listOptions($colors, 'colors'); ?>
<input type="submit" name="submit" value="Submit" />
</form>

 

Many thanks!

Wow, I just figured it out! :D

 

I was creating the dynamically named array incorrectly, it should have been

$$groupName = $options;

 

Here's my working code for others that could use it:

<?php

if(isset($_POST['submit']))
{
$groupName = $_POST['group_name'];

if(isset($_POST[$groupName]))
{
	echo "<br/>\n";
	print_r($_POST[$groupName]);
}
}

$colors = array('red','blue','green','yellow','purple','black','white','orange','pink');

function listOptions($options, $groupName)
{
//create dynamically named array
$$groupName = $options;

$checkBoxName = $groupName . '[]';
$count = 0;

foreach($options as $option)
{
	$id = $groupName . $count;
	echo "\t<p><input type=\"checkbox\" id=\"$id\" name=\"$checkBoxName\" value=\"$option\" /><label for=\"$id\">$option</label></p>\n";
	$count++;
}
}
?>

<h1>Favorite colors:</h1>
<form name="test_form" method="post">
<?php listOptions($colors, 'colors'); ?>
<input type="hidden" name="group_name" value="colors" />
<input type="submit" name="submit" value="Submit" />
</form>

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.