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

Reply

Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

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

 
0
  #1
Sep 15th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 70
Reputation: Diode is on a distinguished road 
Solved Threads: 0
Diode's Avatar
Diode Diode is offline Offline
Junior Poster in Training

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

 
0
  #2
Sep 15th, 2008
I forgot to include that I am using a form, with the POST method.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

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

 
0
  #3
Sep 15th, 2008
Make sure all your form elements have the same name value with an empty bracket.
ie
  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.
  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. }
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

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

 
0
  #4
Sep 16th, 2008
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 )
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

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

 
0
  #5
Sep 16th, 2008
That's a helpful thing to remember Atli. Also just for future reference, let's say you have some checkboxes such as the following:

  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:

  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.#
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC