【问题标题】:Adding working checkboxes to a contact form将工作复选框添加到联系表单
【发布时间】:2015-09-06 18:10:45
【问题描述】:

我正在尝试在我的联系表单中添加复选框。我有一个带有 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();
           ?>

【问题讨论】:

    标签: php html forms checkbox contact


    【解决方案1】:

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

    如果我猜的话,他们的标签就在他们上面。 (请注意,应避免使用此类空元素。)

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

    【讨论】:

    • 我整天都在添加和删除不同的元素,但它确实没有改变任何东西,所以也许更大的问题是你所说的标签位于上方和空元素。我再次按照您所说的添加了“名称”元素,但是当我尝试删除 div 并将标签移到上方时,复选框完全消失了。或者更确切地说,我的标签似乎放在不可点击的盒子上,所以我把东西放回原来的样子。如何在不丢失复选框的情况下使用标签?
    • 使用浏览器的开发工具来发现复选框上方是否有内容。 (右键单击并检查元素,或类似的)。您还可以在标签中输入内容,或删除它们。如果上面有什么东西,那么你可以使用 CSS 来纠正这个问题。
    • 当我将元素更改为以下内容时,这些框是可选的。但是,不是用复选标记标记它们,而是“值”元素的内容自动出现在框中,并且框内容不会出现在电子邮件中。
    • 我已经尝试并尝试正确地在此处发布上面的代码。我不明白。它不会起作用。
    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多