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

I forgot to include that I am using a form, with the POST method.

Make sure all your form elements have the same name value with an empty bracket.
ie

<input type="checkbox" name="checked[]" value="dog" />Dog
<br />
<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.

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 />";
}

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 :))

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

<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:

// 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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.