How to make checkbox info append to email

They have: 9 posts

Joined: Sep 2014

I have 3 checkboxes on my contact form. The form works with exception to the checkboxes.. The boxes are selectable but once hitting the "submit" button the selection shows up on the "Thank you for you comment" page. I cannot figure out how to get the checkbox selection to appear in the actual email.

I have tried all sorts of php functions that I have found people to be using in forums and tutorials. Nothing appends the checkbox info to the email. I can't seem to find anyone who knows how to do this.

The 'implode' function does not work. The 'echo' function does not work. The 'array' function does not work. What could be going on?

Here is my html:

<section class="contact">  
    <form id="contact" action="html_form_send.php" method="post">
    <fieldset title="About you">
    <legend><span>About you</span></legend>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" tabindex="10"  class="txtinput" placeholder="Your name" required>
      <label for="email">Email</label>
      <input type="email" name="email" id="email" tabindex="20" class="txtinput" placeholder="valid email" required>
    </fieldset>
    <fieldset title="Your turn">
    <legend><span>Your turn</span></legend>
<p>What, exactly then, are you after?</p>
<div>
<div class="tag">Checkbox1</div>        
        <input type="checkbox" id="checkbox-1-1" name="option[ ]" class="regular-checkbox big-checkbox" value="salvation" /></input><label for="checkbox-1-1"></label>
</div>
<div>
<div class="tag">Checkbox2</div>
<input type="checkbox" id="checkbox-2-1" name="option[ ]" class="regular-checkbox big-checkbox" value="question" /><label for="checkbox-2-1"></label>
</div>
<div>
<div class="tag">Checkbox3</div>
<input type="checkbox" id="checkbox-3-1" name="option[ ]" class="regular-checkbox big-checkbox" value="other" /><label for="checkbox-3-1"></label>
</div>
       <label for="comment" class="inline"></label>
          <label for="discourse">Write your comments here</label>
         <textarea id="discourse" name="discourse" tabindex="30" class="txtinput" rows="7" placeholder="This is where your thoughts go">
         </textarea>
        
    <section id="buttons">
    <input type="reset" name="reset" id="resetbtn" class="resetbtn" value="Reset">
    <input type="submit" name="submit" id="submitbtn" class="submitbtn" tabindex="40" value="Send away!">
    <br style="clear:both;">
</section>

Here is my php:

<?php
if(isset($_POST['email'])) {
   
   
// CHANGE THE TWO LINES BELOW
   
$email_to = "[email protected]";
   
   
$email_subject = "website feedback";
   
   
    function
died($error) {
       
// your error code can go here
       
echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
        echo
$error."<br /><br />";
        echo
"Please go back and fix these errors.<br /><br />";
        die();
    }
   
   
// validation expected data exists
   
if(!isset($_POST['name']) ||
        !isset(
$_POST['email']) ||
        !isset(
$_POST['discourse'])) {
       
died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
   
   
$name = $_POST['name']; // required
   
$email_from = $_POST['email']; // required
   
$discourse = $_POST['discourse']; // not required    $comments = $_POST['comments']; // required
   
   
$error_message = "";
   
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!
preg_match($email_exp,$email_from)) {
     
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
   
$string_exp = "/^[A-Za-z .'-]+$/";
  if(!
preg_match($string_exp,$name)) {
     
$error_message .= 'The Name you entered does not appear to be valid.<br />';
  }
   if(
strlen($discourse) < 2) {
     
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(
strlen($error_message) > 0) {
     
died($error_message);
  }
   
$email_message = "Form details below.\n\n";
   
    function
clean_string($string) {
     
$bad = array("content-type","bcc:","to:","cc:","href");
      return
str_replace($bad,"",$string);
    }
   
   
$email_message .= "Name: ".clean_string($name)."\n";
   
$email_message .= "Email: ".clean_string($email_from)."\n";
   
$email_message .= "Comments: ".clean_string($discourse)."\n";
   
    if(isset(
$_POST["submit"])) //Checks if the send button is pressed
{
        echo
$_POST["name"]; //print your name
       
echo $_POST["email"]; //print your email
    
        //check all the checkboxes if anyone is checked. this is a lot of work to do.
        //All checkboxes are checked one by one
   
if(isset($_POST["salvation"]))
        echo
$_POST["salvation"];
    
    if(isset(
$_POST["question"]))
        echo
$_POST["question"];
        
    if(isset(
$_POST["other"]))
        echo
$_POST["other"];
}
        if(isset(
$_POST["submit"])) //Checks if the send button is pressed
{
    echo
$_POST["name"]; //print your name
   
echo $_POST["email"]; //print your email
    
   
if(isset($_POST["option"])) //checks if any interest is checked
   
{
        foreach(
$_POST["option"] as $value) //Iterate the interest array and get the values
       
{
            echo 
$value//print the values
       
}
    }
}


   
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@
mail($email_to, $email_subject, $email_message, $headers);  }
?>


<!-- place your own success html below -->
<script language="javascript" type="text/javascript">
alert('Thank you for contacting us.');
window.location = 'feedbackform.html';
</script>
<?php
die();
?>
  

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

The main two things I see...

First one, I'm not sure of, but I think there should be no spaces in the names for the options:

name="option[]" instead of name="option[ ]"

Once you have that setup, you will have an array of $_POST['option'] that will contain the values checked. If you don't check any checkboxes though, this will NOT exist at all.

So in your code you need to do if ( isset( $_POST['options'] ) ) { /* process the array */ }

I was a little bored while watching a movie, and threw this sample together for you. Note, this is designed to be a single file, form on same page that processes it, and submits to itself.

The big plus side to that is it will let you know errors on the form, and auto fill in (and check) what was already submitted instead of an unfriendly "Go Back and try again..." type of thing.

Also since field inputs get repeated (especially if you use this across several forms) I have text inputs and checkbox inputs set up as functions. As you will see, the HTML portion of the code is much cleaner this way.

PS. just noticed when I looked it over, there is a type on line 63, it should say None Selected instead of Non Selected

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.