Skip to main content
November 29, 2007
Answered

checkboxes and array

  • November 29, 2007
  • 5 replies
  • 1001 views
This should be fairly simple but I am stumped...

???I want to display a question and 9 answers with checkboxes which are dynmically checked by an array.

On a form I have a table which has a question and 9 answers (checkboxes)
The checkboxes have the same name (Answer5) so it creates an array when submitted to the results page

Below is the code for the table with the question and answers:

<table width="400" cellpadding="0" cellspacing="0">
<tr><td colspan="2">My Question goes here ?</td></tr>
<tr><td><label> <input type="checkbox" name="Answer5" value="Bedroom Addition">Bedroom Addition</label></td></tr>
<tr><td><label><input type="checkbox" name="Answer5" value="Family room Addition">Family room Addition</label></td></tr>
<tr><td><input type="checkbox" name="Answer5" value="Garage Addition">Garage Addition</td></tr>
<tr><td><input type="checkbox" name="Answer5" value="Solarium/Greenhouse">Solarium/Greenhouse</td></tr>
<tr><td><input type="checkbox" name="Answer5" value="Guest Suite">Guest Suite</td></tr>
<tr><td><input type="checkbox" name="Answer5" value="Sunroom">Sunroom</td></tr>
<tr><td><input type="checkbox" name="Answer5" value="Out Building">Out Building</td></tr>
<tr><td><input type="checkbox" name="Answer5" value="Pool Enclosure">Pool Enclosure</td></tr>
<tr><td><input type="checkbox" name="Answer5" value="2nd Story Addition">2nd Story Addition</td></tr>
</table>

When I click submit it goes to a results page and prints the array.

Below is the code for printing the array:
<?php print_r($HTTP_POST_VARS['Answer5']); ?>

And it displays:

Array ( [0] => Bedroom Addition [1] => Garage Addition )

when i check those checkboxes.


???Instead of displaying the array --its just there for error checking for now--
???I want to display the same table with the question and 9 answers on the results page with the checkboxes that were selected on the previous page

The way I am actually using this is to submit the array to a database. Then have another page the retrieves the array from the database to view the results. I simplified this a bit to get the functionality working right.

I'm stumped! Please Help! Thanks!




when I submit it goes to a results page which simply displays the result

the result is an array.

This topic has been closed for replies.
Correct answer Newsgroup_User
On Fri, 30 Nov 2007 00:45:48 +0000 (UTC), "joez461"
<webforumsuser@macromedia.com> wrote:

> My question is how do I use the array to check or uncheck the same checkboxes
>(dynamically)?

<?php
$Answers=array(
"Bedroom Addition",
"Family room Addition",
"Garage Addition",
"Solarium/Greenhouse",
"Guest Suite",
"Sunroom",
"Out Building",
"Pool Enclosure",
"2nd Story Addition"
);
foreach($Answers as $a){
print "<tr><td><label><input type=\"checkbox\"".
"name=\"Answer5[]\" value=\"$a\"";
if(is_array($_POST['Answer5']) && in_array($a,$_POST['Answer5']))
print ' checked="checked"';
print ">$a</label></td></tr>\n";
}
?>

Gary

5 replies

Inspiring
November 30, 2007
On Fri, 30 Nov 2007 19:41:23 +0000 (UTC), "joez461"
<webforumsuser@macromedia.com> wrote:

>Thanks Gary! That was it.

You're welcome, Joe. Glad it worked for you.

Gary
Participating Frequently
December 6, 2007
I found this thread via Google and it has really helped me sort out an issue that has been holding me up for a while now. Thank you very much!
Mark
November 30, 2007
Thanks Gary! That was it.
Newsgroup_UserCorrect answer
Inspiring
November 30, 2007
On Fri, 30 Nov 2007 00:45:48 +0000 (UTC), "joez461"
<webforumsuser@macromedia.com> wrote:

> My question is how do I use the array to check or uncheck the same checkboxes
>(dynamically)?

<?php
$Answers=array(
"Bedroom Addition",
"Family room Addition",
"Garage Addition",
"Solarium/Greenhouse",
"Guest Suite",
"Sunroom",
"Out Building",
"Pool Enclosure",
"2nd Story Addition"
);
foreach($Answers as $a){
print "<tr><td><label><input type=\"checkbox\"".
"name=\"Answer5[]\" value=\"$a\"";
if(is_array($_POST['Answer5']) && in_array($a,$_POST['Answer5']))
print ' checked="checked"';
print ">$a</label></td></tr>\n";
}
?>

Gary
November 30, 2007
Thanks.

I will make the change.
The array works just fine though.

"
And it displays:

Array ( [0] => Bedroom Addition [1] => Garage Addition )

when i check those checkboxes.
"

My question is how do I use the array to check or uncheck the same checkboxes (dynamically)?
Inspiring
November 29, 2007
On Thu, 29 Nov 2007 21:09:40 +0000 (UTC), "joez461"
<webforumsuser@macromedia.com> wrote:

> On a form I have a table which has a question and 9 answers (checkboxes)
> The checkboxes have the same name (Answer5) so it creates an array when
>submitted to the results page

It won't create an array unless the name includes square brackets:

<input type="checkbox" name="Answer5[]" ...

Change all of those and you'll get an array.

Gary