Page 1 of 1

Passing checkbox values from form

Posted: Fri Sep 12, 2003 7:44 am
by chriso
I've created a form which provides a list of various certifications. The user will place a check in each box for each certification they possess. When submitted, the results will be updated in a mysql table which will contains the user's ID (FID) and the course ID (CID). I'm familiar with how cgi handles this when passed but I'm not sure how to deal with it in php.

I've also considered just passing the user ID via a hidden form tag and making the checkbox values the value of the course ID. I would then use the user's ID in a loop to add all the checked certs CIDs to the table for that user but I'm not sure how to set up the loop for the data that's passed.

I assume php uses key/value pairs like perl, but how do I determine the key/value pair? If I pass the user ID via a hidden form tag, how then would I parse the values of the checkboxes in order to insert the course ID into the db? Will php store the passed values as an array or an associative array (hash?)?

Sorry if I didn't explain this very well. Thanks for the help.

Posted: Fri Sep 12, 2003 8:38 am
by guinness
Say your checkbox looks like this:

<input type="checkbox" name="Course1" value="302" />

You would access that value on the next page like this:

$assignedString = $_POST['Course1'];

This assumes that you are:
1) Using a fairly current version of php 4.0
2) That your form is using the method="Post"

If your version of php is old you may have to access the variables like this:

$assignedString = $HTTP_POST_VARS['Course1'];

Hope this helps.

Posted: Fri Sep 12, 2003 8:41 am
by chriso
That helps except I won't know which checkboxes the user marks. They may mark one or up to seven. If I give the same name to all the name attributes, how would I then identify each value?

Or, if I give different names to each name attribute, again, how would I loop through and identify each key/value?

Posted: Fri Sep 12, 2003 8:59 am
by Taranis
This is how I normally do it. In the HTML, make your checkboxes like this:

<input type="checkbox" name="selected[]" id="selected[]" value="302" />
<input type="checkbox" name="selected[]" id="selected[]" value="303" />
<input type="checkbox" name="selected[]" id="selected[]" value="304" />

If you have other series of checkboxes in the same form, you can use something like:

<input type="checkbox" name="selected_A[]" id="selected_A[]" value="102" />
<input type="checkbox" name="selected_A[]" id="selected_A[]" value="103" />
<input type="checkbox" name="selected_A[]" id="selected_A[]" value="104" />

That way, you can keep the two different sets of selections different. It doesn't have the be called "selected" but it does have to have the brackets. Anyway, this is what you do in the PHP:

Code: Select all

<?php
$selected =& $_POST['selected'];

$selected_A =& $_POST['selected_A']
?>
That puts all the selected values into an array that you can then do with whatever you need. You could feed them to a function that users a foreach loop to stick them in the DB along with the userID.

Posted: Fri Sep 12, 2003 8:59 am
by guinness
Well, you could just give all the checkboxes unique names. Then on the next page, check to see if they're empty or not. Empty means they were unchecked and a value would be present if they were checked.

But being a Java Engineer, I understand what you're saying. I would love to be able to run a hashtable or enumeration for the parameters.

You could do this:

set up an array with the names of your form tags on the submit page, ie:

$formElementArray = array('checkbox1','checkbox2',...etc.);

Then assign all your form tags these array names on the previous page.

On the submit page, run through the array like this:

foreach ($formElementArray as $key => $value)
{
$asssignedString = $_POST[$value];

if (assignedString != "")
{
//grab the hidden userID and insert into db
}
}

Posted: Fri Sep 12, 2003 9:04 am
by chriso
Thanks for the great ideas. I get a little better with each post. Thanks for taking the time to answer.

Posted: Fri Sep 12, 2003 10:10 am
by Taranis
Not sure of your implementation, but the code and ideas I have provided is something I have used on many sites and it works very well. It's quite simple also. Let me know if you need any more help.

Posted: Fri Sep 12, 2003 12:02 pm
by chriso
Taranis wrote:

Code: Select all

<?php
$selected =& $_POST['selected'];

$selected_A =& $_POST['selected_A']
?>
Is the & supposed to be after the = or is this a typo?