treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How to send checkbox selections in a form to email

  • Hi, I am having issues capturing data from the checkboxes in my form. I can't figure out what code needs to go into the php file for multiple selections.

    The rest of the form is fine so I have left out most of the coding. Below is my html code for the checkboxes and my full php code as it stands.

    Any guidance/code would be greatly aprpeciated. Thanks in advance!

    HTML Code

      <div class="row">
      <p>Please select your preferred sessions:</p>
      <div class="check-box">
      <input name="Session" type="checkbox" id="morning" value="Morning" />Morning: 10am - 1pm
      </div> <!--end .check-box -->
      <div class="check-box">
      <input name="Session"  type="checkbox" id="noon" value="Afternoon" />Afternoon: 2pm - 5pm
      </div> <!--end .check-box -->
      <div class="check-box">
      <input name="Session"  type="checkbox" id="evening" value="Evening" />Evening: 7pm - 10pm
      </div> <!--end .check-box -->
      </div> <!--end .row -->
    

    PHP Code <?php $EmailFrom = "myemail@email.com"; $EmailTo = "myemail@email.com"; $Subject = "Registration; $Fullname = Trim(stripslashes($_POST['Fullname'])); $Email = Trim(stripslashes($_POST['Email'])); $Session = Trim(stripslashes($_POST['Session'])); // validation $validationOK=true; if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">"; exit; } // prepare email body text $Body = ""; $Body .= "Fullname: "; $Body .= $Fullname; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Session: "; $Body .= $Session; $Body .= "\n"; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); // redirect to success page if ($success){ print ""; } else{ print ""; } ?>

  • Try putting the code in the given "Code" tag when posting. That post is a jumbled mess and I don't even want to attempt to decipher it! :P

  • Fixed some of it.

  • Checkbox's return yes or no as a value. You would have to have 3 unique names for each checkbox and use PHP to see which returned YES or NO.