Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

PHP - Checkboxes, mail() and foreach() - getting only last checkbox value...

Here is the HTML

I have a large form to process and send through email, part of which has a set of 6 checkboxes named "experience" with six different values...

Here is the PHP code in question...

      foreach($_POST['expertise'] as $value)
                  $expert .= "$value   ";
      if( mail($mail_to,$mail_subject,
            "\tTitle: ".$title.
            "\n\tName: ".$name.
            "\n\tEmail: ".$email.
            "\n\n\tCompany: ".$company.
            "\n\n\tAddress: ".$address.
            "\n\tCity: ".$city.
            "\n\tState / Province: ".$province.
            "\n\tPostal Code: ".$postal.
            "\n\n\t Phone Number: ".$phone."   Extension: ".$extension.
            "\n\tCell: ".$cell.
            "\n\tFax: ".$fax.
            "\n\nExperience: \n====================\n".$experience.
            "\n\nArea(s) of Expertise: \n====================\n".$expert.
            "\n\nCapabilities: \n====================\n".$capabilities.
            "\n\nProfessional Designations: \n====================\n".$designations.
            "\n\nIndustry Sector(s): \n====================\n".$sectors.
            "\n\nComments / Questions: \n====================\n".$comments,
            "From: ".$email."\r\n".
            "Reply-To: ".$email."\r\n") )

I thought the foreach() code would do the trick, but I only get the last checkbox value selected to appear in the email. I used the "." operator so it would add on the values to the variable, so I don't know what i'm doing wrong here. The rest of the form works fine.

Thanks in advance for your help.

1 Solution
Are you sure you aren't refering to a field named expertise (going  by your PHP Code)

if so change something like

<input type="checkbox" name="expertise" value="whatever">

to something like

<input type="checkbox" name="expertise[]" value="whatever">

Note that I'm changing experience to expertise[] by adding square brackets.

Then you can

$expert = implode(" and ", $expertise); // or something like that.
chandldjAuthor Commented:
You are absolutely right... i'm a bonehead - duh... I was trying to do this for the experience variable...

The implode function worked great...




Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now