Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

checkboxes and array

Guest
Nov 29, 2007 Nov 29, 2007

Copy link to clipboard

Copied

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.

TOPICS
Server side applications

Views

948
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 30, 2007 Nov 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_...

Votes

Translate
LEGEND ,
Nov 29, 2007 Nov 29, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 29, 2007 Nov 29, 2007

Copy link to clipboard

Copied

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)?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 30, 2007 Nov 30, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 30, 2007 Nov 30, 2007

Copy link to clipboard

Copied

Thanks Gary! That was it.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 30, 2007 Nov 30, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

On Thu, 6 Dec 2007 12:28:14 +0000 (UTC), "markbloomfield"
<webforumsuser@macromedia.com> wrote:

>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!

You're welcome, Mark.

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 06, 2007 Dec 06, 2007

Copy link to clipboard

Copied

Hello Gary,
I have been trying to use the code to set the values of the checkboxes from a string held in a MySQL database. I am sure I am missing something quite basic but have been looking at it for so long that I've got a form of snow blindness.
The code I am using is below, where the Array for $Answers is replaced by $AllKeywordList which is an Array created from the string $row_rsAllKeywords['AllKeywords'].
The string $NewKeywordList is created from the Array $List to print into a text field for now to then update my database with the new values from the selection.
I have tried to set the variable $List from the database by exploding a string of the selected values that have been stored, which works fine in populating the text field, but it will not set the checkbox values.
I am sure that I am missing something very basic, but as I said I cannot see it.
Any ideas?
Best wishes,
Mark

<?php $KeywordList = $List;
sort($KeywordList);?>

<?php $AllKeywords = explode (",",$row_rsAllKeywords['AllKeywords']);?>

<?php $AllKeywordList = $AllKeywords;
sort($AllKeywordList);?>

<?php
foreach($AllKeywordList as $Line[value]){
print "<tr><td><label><input type=\"checkbox\"".
"name=\"List[]\" value=\"$Line[value]\"";
if(is_array($_POST['List']) && in_array($Line[value],$_POST['List']))
print ' checked="checked"';
print ">$Line[value]</label></td></tr>\n";
}
?>

<?php $NewKeywordList = implode (",",$List);?>

<input name="text" type="text" id="area" value="<?php print ("$NewKeywordList");?>" size="80" />
<?php $AllKeyWordsList = implode (",",$SelectAllKeywords);?>
<input type="submit" name="button" id="button" value="Submit" />
<input type="hidden" name="MM_update" value="form1" />
</form>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 07, 2007 Dec 07, 2007

Copy link to clipboard

Copied

On Fri, 7 Dec 2007 07:36:59 +0000 (UTC), "markbloomfield"
<webforumsuser@macromedia.com> wrote:

> Any ideas?

Not sure what the rest of the page looks like, but perhaps this will get
you headed in the right direction:

<?php
$KeywordList = $_POST['List'];
if(is_array($KeywordList))
sort($KeywordList);

$AllKeywords = explode (",",$row_rsAllKeywords['AllKeywords']);

sort($AllKeywords);
?>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post">
<?php
print "<table>\n";
foreach($AllKeywords as $value){
print "<tr><td><label><input type=\"checkbox\"".
"name=\"List[]\" value=\"$value\"";
if(is_array($KeywordList) && in_array($value,$KeywordList))
print ' checked="checked"';
print ">$value</label></td></tr>\n";
}
print "</table>\n";

if(is_array($KeywordList))
$NewKeywordList = implode (",",$KeywordList);
?>
<input name="text" type="text" id="area" value="<?php
print $NewKeywordList;?>" size="80" />
<?php //$AllKeyWordsList = implode (",",$SelectAllKeywords);?>
<input type="submit" name="button" id="button" value="Submit" />
<input type="hidden" name="MM_update" value="form1" />
</form>

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 07, 2007 Dec 07, 2007

Copy link to clipboard

Copied

Hello Gary,
Thanks for getting back to me so quickly. This seems to work but what I need to do is work out how to use a value for $List that is derived from a string that is in my database field. Rather than the value returned in the form.
<?php $List = explode(",",$row_Recordset1['searchkeywords']);?>
Where 'searchkeywords' is the field to which the string of associated keywords has been written.
When I use this code the form field is populated but it cannot check the values of the checkboxes. Not sure why this is the case.
Mark

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 07, 2007 Dec 07, 2007

Copy link to clipboard

Copied

On Fri, 7 Dec 2007 19:04:53 +0000 (UTC), "markbloomfield"
<webforumsuser@macromedia.com> wrote:

>Hello Gary,
> Thanks for getting back to me so quickly. This seems to work but what I need
>to do is work out how to use a value for $List that is derived from a string
>that is in my database field. Rather than the value returned in the form.
> <?php $List = $row_Recordset['searchkeywords'];?>
> Where 'searchkeywords' is the field to which the string of associated keywords
>has been written.

I thought the values returned by the recordset were in AllKeywords:

$AllKeywords = explode (",",$row_rsAllKeywords['AllKeywords']);

Perhaps if you post more code, someone can figure out your problem. With
the snippet you posted, I cannot.

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 07, 2007 Dec 07, 2007

Copy link to clipboard

Copied

The value that creates all the checkboxes is $AllKeywords. This is created from a string in a database field.
What I want to achieve is when the page loads for the first time there is a list of checkboxes some of which are already checked. The value for these checked boxes will be derived from a string in a database field that holds keywords associated with a particular record this is the value $row_Recordset['searchkeywords'] and I tried creating an array from it and calling it $List but this doesn't work. All my code is below which is largely the code revision you posted earlier: I hope this make sense.

<?php $List = explode(",",$row_Recordset['searchkeywords']);//create array from recordset query?>
<?php print $row_Recordset['searchkeywords'];//Print string from recordet query?>
<?php print $List;// print Array?>
<?php $List2 = implode(",",$List);// create string from array $List?>
<?php print $List2;// Print string created from array $List to see value?>

<?php
$KeywordList = $_POST['List'];
if(is_array($KeywordList))
sort($KeywordList);

$AllKeywords = explode (",",$row_rsAllKeywords['AllKeywords']);

sort($AllKeywords);
?>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post">
<?php
print "<table>\n";
foreach($AllKeywords as $value){
print "<tr><td><label><input type=\"checkbox\"".
"name=\"List[]\" value=\"$value\"";
if(is_array($KeywordList) && in_array($value,$KeywordList))
print ' checked="checked"';
print ">$value</label></td></tr>\n";
}
print "</table>\n";

if(is_array($KeywordList))
$NewKeywordList = implode (",",$KeywordList);
?>
<input name="text" type="text" id="area" value="<?php
print $NewKeywordList;?>" size="80" />
<?php //$AllKeyWordsList = implode (",",$SelectAllKeywords);?>
<input type="submit" name="button" id="button" value="Submit" />
<input type="hidden" name="MM_update" value="form1" />
</form>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 07, 2007 Dec 07, 2007

Copy link to clipboard

Copied

On Fri, 7 Dec 2007 23:37:23 +0000 (UTC), "markbloomfield"
<webforumsuser@macromedia.com> wrote:

>The value that creates all the checkboxes is $AllKeywords. This is created from
>a string in a database field.

Okay, I think I have the idea. You didn't include the recordset code, so
I just used a couple of dummy strings here:

<?php
$row_Recordset['searchkeywords']="Blue,Green";
$row_rsAllKeywords['AllKeywords']="Red,Blue,Green,Cyan,Magenta,Yellow,Black";

$List = explode(",",$row_Recordset['searchkeywords']);//create array
from recordset query
print $row_Recordset['searchkeywords'];
print $List;// print Array
$List2 = implode(",",$List);// create string from array $List
print $List2;// Print string created from array $List to see value

$KeywordList = $_POST['List'];
if(is_array($KeywordList))
sort($KeywordList);

$AllKeywords = explode (",",$row_rsAllKeywords['AllKeywords']);

sort($AllKeywords);
?>
<form action="<?php print $_SERVER['PHP_SELF'];?>" method="post">
<?php
print "<table>\n";
foreach($AllKeywords as $value){
print "<tr><td><label><input type=\"checkbox\"".
"name=\"List[]\" value=\"$value\"";
if(is_array($List) && in_array($value,$List))
print ' checked="checked"';
print ">$value</label></td></tr>\n";
}
print "</table>\n";

if(is_array($List))
$NewKeywordList = implode (",",$List);
?>
<input name="text" type="text" id="area" value="<?php
print $NewKeywordList;?>" size="80" />
<?php //$AllKeyWordsList = implode (",",$SelectAllKeywords);?>
<input type="submit" name="button" id="button" value="Submit" />
<input type="hidden" name="MM_update" value="form1" />
</form>

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2007 Dec 08, 2007

Copy link to clipboard

Copied

Gary,
Thank you for your help with this one. I think that has cracked it by pulling everything together. I have set the following:

if (empty($List)) {
$List = explode(",",$row_Recordset['searchkeywords']);
}

Which will enable the boxes to be checked either from the submitted form or from the database when accessed for the first time.
I am updating the database from the submitted form. It seems to work OK although I will need to work out whether I can submit the checked checkbox values directly rather than through the text box as I do at present.
Once again thank you for your time it has really given me a lot of help.
With best wishes,
Mark

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 08, 2007 Dec 08, 2007

Copy link to clipboard

Copied

LATEST
On Sat, 8 Dec 2007 08:38:21 +0000 (UTC), "markbloomfield"
<webforumsuser@macromedia.com> wrote:

> I am updating the database from the submitted form. It seems to work OK
>although I will need to work out whether I can submit the checked checkbox
>values directly rather than through the text box as I do at present.

Sure. Just join() or implode() the $_POST['List'] values.


> Once again thank you for your time it has really given me a lot of help.
> With best wishes,

You're welcome. Good luck!

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines