943,682 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2438
  • PHP RSS
Sep 15th, 2008
0

Many checkboxes in form, how do I check which ones are checked?

Expand Post »
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
Similar Threads
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 15th, 2008
0

Re: Many checkboxes in form, how do I check which ones are checked?

I forgot to include that I am using a form, with the POST method.
Reputation Points: 50
Solved Threads: 0
Junior Poster in Training
Diode is offline Offline
70 posts
since Jan 2005
Sep 15th, 2008
0

Re: Many checkboxes in form, how do I check which ones are checked?

Make sure all your form elements have the same name value with an empty bracket.
ie
html Syntax (Toggle Plain Text)
  1. <input type="checkbox" name="checked[]" value="dog" />Dog
  2. <br />
  3. <input type="checkbox" name="checked[]" value="cat" />Cat
Then, in your processing script, just loop through the checkbox array and do what you need to inside the loop.
php Syntax (Toggle Plain Text)
  1. foreach($_POST['checked'] as $value){
  2. //do whatever here. I'll just print the values but you can update the db, push into an array, or whatever here.
  3. print $value;
  4. print "<br />";
  5. }
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Sep 16th, 2008
0

Re: Many checkboxes in form, how do I check which ones are checked?

To add a to buddylee's post.
Note that if a checkbox isn't checked, it will not be sent, and will therefore not be included in the $_POST array.
(May seem obvious, but there is no harm in stating the obvious )
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Sep 16th, 2008
0

Re: Many checkboxes in form, how do I check which ones are checked?

That's a helpful thing to remember Atli. Also just for future reference, let's say you have some checkboxes such as the following:

PHP Syntax (Toggle Plain Text)
  1. <fieldset>
  2. <legend>Optionals (Un-Related Checkboxes):</legend>
  3. <label for="mailingList">I would like to be added to your mailing list:</label>
  4. <input type="checkbox" name="mailingList" id="mailingList" value="Yes" />
  5. <br />
  6. <label for="thirdParty">I agree to my details being passed onto third parties:</label>
  7. <input type="checkbox" name="thirdParty" id="thirdParty" value="Yes" />
  8. </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)
  1. // If the user didn't choose to join the mailing list, set the variable to 'No' so it can be put into the database.
  2. if (!isset($_POST['mailingList'])) {
  3. $mailingList = "No";
  4. }
  5.  
  6. // If the user didn't accept third parties, set the variable to 'No' so it can be put into the database.
  7. if (!isset($_POST['thirdParty'])) {
  8. $thirdParty = "No";
  9. }

Hope this helps!
Last edited by antwan1986; Sep 16th, 2008 at 6:27 am. Reason: Some Typos.#
Reputation Points: 14
Solved Threads: 8
Junior Poster
antwan1986 is offline Offline
110 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Parse error: syntax error, unexpected T_STRING
Next Thread in PHP Forum Timeline: email





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC