2015-06-21 114 views
0

我试图将复选框添加到我的联系表单中。我有一个有3个复选框的工作联系表单。当我在本页找到的php代码(​​)时,我的复选框不再是可点击的,但是当电子邮件发送电子邮件中出现所有三个复选框时。我认为在某个地方必须有一个简单的解决方案,但是我已经花了整整一天的时间来研究这个问题,而且这一切都是我所能得到的。将工作复选框添加到联系人表单

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

这里是我的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 = "[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._%-][email protected][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(); 
      ?> 

回答

0

如果复选框不能点击则是最有可能是一些其他的元素(一个或多个)坐在上面摆着。使用浏览器的开发人员工具是发现此问题的最快捷方式。 PHP不会影响它们是否可点击。

如果我猜测这将是他们的标签坐在他们上面。 (请注意,应避免使用像这样的空元素。)

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

+0

我已经添加并删除了不同的元素整天,它确实没有改变任何东西,所以也许更大的问题是关于你说的标签坐在上面和空元素。我再次添加了“name”元素,就像你说的那样,但是当我试图移除div并移动上面的标签时,那么复选框就完全消失了。或者说,我的标签看起来像是放在了不可点击的盒子上,所以我把它们放回原来的样子。如何在不丢失复选框的情况下使用标签? – Dre

+0

使用浏览器的开发人员工具来发现复选框上方是否有内容。 (右键单击并检查元素,或类似)。您也可以在标签中键入内容,或将其删除。如果上面有东西,那么你使用CSS来纠正这个问题。 –

+0

当我将元素更改为以下内容时,这些框可以选择。但是,并非用复选标记标记它们,“值”元素的内容会自动显示在框中,并且框内容不会显示在电子邮件中。 – Dre