Page 1 of 1
dynamic checkbox values
Posted: Mon Jul 02, 2007 4:03 am
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
Posted: Mon Jul 02, 2007 4:34 am
by Oren
Posted: Tue Jul 03, 2007 5:20 am
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?
Posted: Tue Jul 03, 2007 5:39 am
by miro_igov
Code: Select all
$_SESSION["dbChkSession"]=$_REQUEST["chkSession"];
Posted: Tue Jul 03, 2007 7:39 am
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.
Posted: Tue Jul 03, 2007 7:41 am
by feyd
in_array() may be of interest.
Posted: Tue Jul 03, 2007 10:57 am
by aceconcepts
Superb!
Thanks Feyd.