2015-06-21 32 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