0

I am trying to add checkboxes to my contact form. I have a working contact form that with 3 checkboxes. When I place the php code that I found on this page (send checkbox value in PHP form) then my checkboxes are no longer clickable BUT when the email sends all three checkboxes appear in the email. I think that there must be a simple fix somewhere but I have spent the whole live long day working on it and this is as far as I have been able to get.

How do I get the checkboxes clickable AND have the selected box be included in the email?

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" class="regular-checkbox big-checkbox" value="Yes" />
   <label for="salvation"></label>
  </div>    
  <div>
    <div class="tag">checkbox2</div>
    <input type="checkbox" id="checkbox-2" class="regular-checkbox big-checkbox" value="Yes" />
    <label for="question"></label>
  </div>    
  <div>
    <div class="tag">checkbox3</div>
    <input type="checkbox" id="checkbox-3" class="regular-checkbox big-checkbox" value="Yes" />
    <label for="other"></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>

And here is my php

          <?php
if(isset($_POST['email'])) {

          // CHANGE THE TWO LINES BELOW
          $email_to = "myemail@email.org";

          $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 .= "checkbox1: ".clean_string($checkbox-1)."\n";
          $email_message .= "checkbox2: ".clean_string($checkbox-2)."\n";
          $email_message .= "checkbox3: ".clean_string($checkbox-3)."\n";
          $email_message .= "Comments: ".clean_string($discourse)."\n";

          $checkbox1= $_POST['checkbox1'];
          if ($checkbox1!= 'Yes') {
          $checkbox1= 'No'; }

          $checkbox2= $_POST['checkbox2'];
         if ($checkbox2!= 'Yes') {
         $checkbox2= 'No'; }

         $checkbox3= $_POST['checkbox3'];
         if ($checkbox3!= 'Yes') {
         $checkbox3= 'No'; }

         // 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);  
         ?>

          <?php
         $checkbox = $_POST['value']
          //Will return either "checkbox1" or "checkbox2" or "checkbox3".
          ?>

         <!-- 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();
           ?>
Community
  • 1
  • 1
Dre
  • 1
  • 2

1 Answers1

0

If the checkboxes aren't clickable then it is most likely that some other element(s) are sitting above them. Using your browser's developer tools is the quickest way to discover this. The PHP does not affect whether they are clickable or not.

If I were to guess it would be that their labels are sitting above them. (Note that empty elements like these should be avoided.)

The checkboxes (and other form elements) must have a name attribute in order to be submitted with the form.

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • I have added and removed different elements all day and it really has not changed anything so maybe the bigger problem is about what you said that the labels are sitting above and empty elements. I again added the "name" element as you said to do, but when I tried to remove the divs and move the label above instead then the checkboxes disappeared altogether. Or rather it appeared that my labels were sitting on unclickable boxes so I put things back the way they originally were. How can I use the label without losing the checkbox? – Dre Jun 21 '15 at 03:50
  • Use your browser's developer tools to discover whether something is above the checkboxes. (Right click and Inspect Element, or similar). You could also type something in the labels, or remove them. If something is above them then you use CSS to correct this. – Andy G Jun 21 '15 at 10:10
  • When I change the elements to the following then the boxes are selectable. However, instead of marking them with a checkmark, the content of the "value" element automatically appears in the box and the box contents do not appear in the email. – Dre Jun 21 '15 at 15:12
  • – Dre Jun 21 '15 at 15:24
  • I have tried and tried to make code above post here correctly. I don't get it. It won't work. – Dre Jun 21 '15 at 15:26
  • The `type` should be `checkbox`, you seem to be using class-names. – Andy G Jun 21 '15 at 17:32
  • I found another good example of what I am trying to do. I keep trying different things but nothing is working right. With this example I have the checkboxes working but I removed the php for them because I was getting error messages. – Dre Jun 21 '15 at 18:31
  • Which buildings do you want access to?
    Acorn Building
    Brown Hall
    Carnegie Complex
    – Dre Jun 21 '15 at 18:32
  • I have spent another whole day working on this and gotten nowhere. No matter what I try, or whose code I try from all the examples on the internet, the selected checkbox info does NOT append to the email. So there must be something else going on since this coding works for other people. – Dre Jun 21 '15 at 20:50
  • Presumably your code has changed a lot since your original post? (At least, it should now include the name attributes.) In which case, you might start a new question, so that we can see more clearly the code you are now working with. Unfortunately, there is a chance that it might be closed as a duplicate, but I think it should have changed significantly enough to prevent this. (I can't guarantee it!) – Andy G Jun 21 '15 at 21:07