dynamic checkbox values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

dynamic checkbox values

Post by aceconcepts »

Hi,

I have a list of questions using checkboxes that are populated from a database.

As I will never always know which questions are going to be 'ticked', how can I determine which values are to be written to the database once the form has been submitted?

Thanks
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Hi,

Thanks for that post.

I have now implemented that segment of code and what I'm now trying to do is re-populate the checkboxes after a form submit so that the user doesn't have to select them again.

Here is the checkbox element:

Code: Select all

<input  type="checkbox" value="<? echo $intSessionId; ?>" name="chkSession[]" />
This is the code used to retrieve the checked boxes on submit:

Code: Select all

$_SESSION["dbChkSession"]=$_REQUEST["chkSession[]"];
I have a feeling that the variable setting is dreadfully wrong...mainly because it doesn't work!!!

How would I retrieve the user selection of multiple checkboxes once they submit a form and then how would I store those selections in a php variables so that the checkboxes can be re-populated?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

$_SESSION["dbChkSession"]=$_REQUEST["chkSession"];
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

I thought that would suffice.

I also solved my next issue. That issue being: how to re-populate a form of checkboxes.

Here it is:

Assume database connection is called

Code: Select all

<input style="width:15px; border:0;" type="checkbox" value="<? echo $intSessionId; ?>" name="chkSession[]"
<? 
foreach($_SESSION['dbChkSession'] as $value) 
{
    if($value==$intSessionId)
    {
            echo "checked=\"checked\"";
    }
} 
?> />
This segment of code is placed inside a loop, so the "if" statement is used to match a value if one exists.

Works perfectly.

Thanks for your support.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in_array() may be of interest.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Superb!

Thanks Feyd.
Post Reply