| | |
Many checkboxes in form, how do I check which ones are checked?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I am making a section in the users' profile where they pick their favorite teams, which might be in different leagues (NCAA, MLB, NFL).
When the user picks the league, it takes them to the next page that lists the teams under their respective conferences (Big 10, Big 12, NFC, AFC, National League, American League, etc.)
Each team has a checkbox, so that they can pick more than one favorite team.
My dilemma is on the action page, how do I figure out which teams have been checked? Do I have to send a field containing the number of teams in the league, and then do a loop through each one to see if the current team in the loop was checked, or is there an easier way?
Thanks
When the user picks the league, it takes them to the next page that lists the teams under their respective conferences (Big 10, Big 12, NFC, AFC, National League, American League, etc.)
Each team has a checkbox, so that they can pick more than one favorite team.
My dilemma is on the action page, how do I figure out which teams have been checked? Do I have to send a field containing the number of teams in the league, and then do a loop through each one to see if the current team in the loop was checked, or is there an easier way?
Thanks
Make sure all your form elements have the same name value with an empty bracket.
ie Then, in your processing script, just loop through the checkbox array and do what you need to inside the loop.
ie
html Syntax (Toggle Plain Text)
<input type="checkbox" name="checked[]" value="dog" />Dog <br /> <input type="checkbox" name="checked[]" value="cat" />Cat
php Syntax (Toggle Plain Text)
foreach($_POST['checked'] as $value){ //do whatever here. I'll just print the values but you can update the db, push into an array, or whatever here. print $value; print "<br />"; }
Lost time is never found again.
- Benjamin Franklin
- Benjamin Franklin
That's a helpful thing to remember Atli. Also just for future reference, let's say you have some checkboxes such as the following:
These are simple check boxes and if the person doesn't want to sign up to anything, they don't check the box. As Atli said, this means that if they're not checked then they don't get sent to the processing script, so a helpful way of processing them if you need to use it in a MySQL table or in an email is this:
Hope this helps!
PHP Syntax (Toggle Plain Text)
<fieldset> <legend>Optionals (Un-Related Checkboxes):</legend> <label for="mailingList">I would like to be added to your mailing list:</label> <input type="checkbox" name="mailingList" id="mailingList" value="Yes" /> <br /> <label for="thirdParty">I agree to my details being passed onto third parties:</label> <input type="checkbox" name="thirdParty" id="thirdParty" value="Yes" /> </fieldset>
These are simple check boxes and if the person doesn't want to sign up to anything, they don't check the box. As Atli said, this means that if they're not checked then they don't get sent to the processing script, so a helpful way of processing them if you need to use it in a MySQL table or in an email is this:
PHP Syntax (Toggle Plain Text)
// If the user didn't choose to join the mailing list, set the variable to 'No' so it can be put into the database. if (!isset($_POST['mailingList'])) { $mailingList = "No"; } // If the user didn't accept third parties, set the variable to 'No' so it can be put into the database. if (!isset($_POST['thirdParty'])) { $thirdParty = "No"; }
Hope this helps!
Last edited by antwan1986; Sep 16th, 2008 at 6:27 am. Reason: Some Typos.#
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
![]() |
Similar Threads
- Problem in checkboxes set (C#)
- Check the state of a check box from a different form (VB.NET)
- Validating a Check Box (JavaScript / DHTML / AJAX)
- checkboxes / enabling fields (Visual Basic 4 / 5 / 6)
- How to pre-select checkboxes in form? (PHP)
- php coding for checkboxes (PHP)
- Processing Checkbox Information From A Form (JavaScript / DHTML / AJAX)
- problem in passing multiple checkbox values (ASP)
- how to validate checkbox with different names? (HTML and CSS)
- Javascript, Form fields validation and submit (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: Parse error: syntax error, unexpected T_STRING
- Next Thread: email
| Thread Tools | Search this Thread |
# 5.2.10 ajax apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date display dissertation dynamic echo echo$_get[x]changingitintovariable... email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link local login loop mail memberships menu mlm multiple multipletables mysql mysqlquery oop open paypal pdf persist php problem query radio random recursion regex remote rss script search server sessions sms soap sockets source space spam sql syntax system table tutorial update upload url validator variable video web xml youtube






)