【问题标题】:How to make checkbox info append to email如何使复选框信息附加到电子邮件
【发布时间】:2015-06-21 21:34:15
【问题描述】:

我的联系表单上有 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-1" name="option[ ]" class="regular-checkbox big-checkbox" value="salvation" /></input><label for="checkbox-1-1"></label>
   </div>
   <div>
        <div class="tag">Checkbox2</div>
        <input type="checkbox" id="checkbox-2-1" name="option[ ]" class="regular-checkbox big-checkbox" value="question" /><label for="checkbox-2-1"></label>
  </div>
  <div>
        <div class="tag">Checkbox3</div>
        <input type="checkbox" id="checkbox-3-1" name="option[ ]" class="regular-checkbox big-checkbox" value="other" /><label for="checkbox-3-1"></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@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 .= "Comments: ".clean_string($discourse)."\n";

    if(isset($_POST["submit"])) //Checks if the send button is pressed
    {
        echo $_POST["name"]; //print your name
        echo $_POST["email"]; //print your email

        //check all the checkboxes if anyone is checked. this is a lot of work to do.
        //All checkboxes are checked one by one
    if(isset($_POST["salvation"]))
        echo $_POST["salvation"];

    if(isset($_POST["question"]))
        echo $_POST["question"];

    if(isset($_POST["other"]))
        echo $_POST["other"];    }
        if(isset($_POST["submit"])) //Checks if the send button is pressed
   {
    echo $_POST["name"]; //print your name
    echo $_POST["email"]; //print your email

    if(isset($_POST["option"])) //checks if any interest is checked
    {
        foreach($_POST["option"] as $value) //Iterate the interest array and get the values
        {
            echo  $value;  //print the values          }         }     }


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

  <!-- 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


    【解决方案1】:
            <div class="tag">Checkbox2</div>
            <input type="checkbox" id="checkbox-2-1" name="option1" class="regular-checkbox big-checkbox" value="yes" /><label for="checkbox-2-1"></label>
      </div>
    

    尝试命名每个选项复选框,然后按如下方式检索它们:

    if(isset($_POST['option1']) && $_POST['option1'] == 'yes'){
        echo "Yes, this was checked";
    }else{
        echo "No, this wasn't checked";
    }    
    

    if 语句的作用是检查该选项是否已设置、含义是否具有值,如果已设置则获取该值。然后它相应地输出

    【讨论】:

    • 我收到以下错误:“解析错误:语法错误,意外”指向 在 php 页面的底部。我一整天都多次收到同样的错误。然而,我的 php 页面没有错误,直到我为这些复选框添加代码。
    • 我不会直接喂你,因为最好学习。但是,我的代码正在演示如何检查这些框。您可能需要对其进行一些操作以直接适合您的代码。
    • 哦,我意识到我必须使代码适合我的表单。我添加了另一个“}”,因为我认为缺少一个。现在没有错误消息,但是,复选框信息未附加到电子邮件中。它再次出现在“感谢您的评论”页面上。电子邮件仍然有效,但它只包含姓名、电子邮件和评论——没有复选框信息。
    • “消息”文本框区域是否会干扰复选框?我想知道,因为虽然电子邮件包含上述信息,但“感谢您的评论”页面显示名称、电子邮件、和复选框信息,但没有评论信息。然而,写在消息框中的文本正在与电子邮件一起发送。发生了一些事情,但我不知道是什么或为什么。
    • 我将此字符串添加到我的 php 中,现在复选框 1、2 和 3 显示在电子邮件中,但没有显示值,或者选择了哪个。 $email_message .= "checkbox-1-1: ".clean_string($option1)."\n";
    猜你喜欢
    • 2016-10-21
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 2015-12-05
    • 1970-01-01
    • 2016-01-08
    相关资源
    最近更新 更多