将工作复选框添加到联系表单

时间:2015-06-21 01:42:27

标签: php html forms checkbox contact

我正在尝试将复选框添加到我的联系表单中。我有一个工作联系表格,有3个复选框。当我放置在此页面上找到的php代码(send checkbox value in PHP form)时,我的复选框不再可点击但是当电子邮件发送时,所有三个复选框都出现在电子邮件中。我认为必须在某个地方进行简单的修复,但我已经花了整整一天的时间来处理它,这是我能够得到的。

如何让复选框可以点击并将所选的框包含在电子邮件中?

这是我的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>

这是我的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();
           ?>

1 个答案:

答案 0 :(得分:0)

如果复选框不可点击,则很可能其他一些元素位于其上方。使用浏览器的开发人员工具是发现这一点的最快捷方式。 PHP不会影响它们是否可点击。

如果我猜测他们的标签就在他们上方。 (请注意,应避免使用这些空元素。)

复选框(和其他表单元素)必须具有name属性才能与表单一起提交。