如何通过多个复选框值通过PHP发送到电子邮件 [英] How to send through multiple checkbox values to an email via PHP

查看:87
本文介绍了如何通过多个复选框值通过PHP发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示了我的HTML文件的摘录.表单目标是,当用户选中一个复选框时,它将在初始总值上添加另一个值.

Below shows an extract of my HTML file. The forms objective was when a user selects a checkbox it would add a further value onto a starting total value.

<!doctype html>  

 <html  class="">  
  <head>

  <script type="text/javascript">
    function checkTotal() {
        document.listForm.total.value = '';
        var sum = 68.50;
        for (i=0;i<document.listForm.choice.length;i++) {
          if (document.listForm.choice[i].checked) {
            sum = sum + parseFloat(document.listForm.choice[i].value);
          }
        }
        document.listForm.total.value = sum.toFixed(2);
    }
</script>

  </head>

  <body>


   <form name="listForm" method="post" action="standard_form.php">

  <div class="form-group">
   <label for="additional_extras">Extras</label><br><br>
<input type="checkbox" name="choice" value="60.00" onchange="checkTotal()"> Number1 </div>

  <div class="form-group">
   <label for="additional_extras_2"></label><br>
<input type="checkbox" name="choice" value="20.00" onchange="checkTotal()"> Number 2
</div>

 <div class="form-group">
   <label for="additional_extras_3"></label><br>
<input type="checkbox" name="choice" value="45.00" onchange="checkTotal()"> Number 3
</div>

 <div class="form-group">
   <label for="additional_extras_4"></label><br>
<input type="checkbox" name="choice" value="23.50" onchange="checkTotal()"> Number 4
</div>

<div class="form-group">
   <label for="additional_extras_5"></label><br>
<input type="checkbox" name="choice" value="40.00" onchange="checkTotal()"> Number 5
</div>

<div class="form-group">
   <label for="additional_extras_6"></label><br>
<input type="checkbox" name="choice" value="23.50" onchange="checkTotal()"> Number 6
</div>

<div class="form-group">
   <label for="additional_extras_7"></label><br>
<input type="checkbox" name="choice" value="120.00" onchange="checkTotal()"> Number 7

<br>
  <h3>Your Current Package Total: £<input type="text" readonly placeholder="68.50"  name="total" id="total"/></h3>

  <button type="submit" class="btn btn-default">Submit</button>  

</form>   

     </div>

  </body>
</html>

然后我将此表单发送到以下php文件,该文件随后将通过电子邮件将表单输出发送出去.我设法使所有其他字段都成功发送,但是在没有添加功能(使总数成为中断)的情况下,无法获取要发送和通过电子邮件输出的复选框值.

I would then send this form to the following php file which would then email the forms outputs. I have managed to get all other fields to send successfully but cannot get the check box values to send and be outputted by email without the adding function (which makes the total) becoming interrupted.

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "myemail";
    $email_subject = "mysubject";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

        $choice = $_POST['choice']; // required
        $total = $_POST['total']; // required

    $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 .= "Choice: ".clean_string($choice)."\n";
    $email_message .= "Order Total: ".clean_string($total)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

请问有人对此有解决方案吗?我尝试过的所有操作都无法让复选框通过那里的值发送,并且像表格的其余部分一样在电子邮件中显示自己.

Please does anyone have a solution for this?? What ever I try I just cant get the check boxes to send through there values and display themselves in the email like the rest of the form.

任何帮助将不胜感激!

谢谢!

推荐答案

您正在构建具有相同名称的复选框输入.这意味着PHP将处理表单提交并不断用新副本(例如

You're building your checkbox inputs all with the SAME name. This means that PHP will process the form submission and continually overwrite "previous" versions of a particular field name:value pair with new copies, e.g

<input type="text" name="foo" value="foo" />
<input type="text" name="foo" value="bar" />
<input type="text" name="foo" value="baz" />

(请注意相同的名称,3个不同的值).只有baz会显示在$ _POST/$ _ GET中,因为它是表单中foo名称的最后一个副本.

(note same name, 3 different values). Only baz will show up in $_POST/$_GET, because it's the last copy of the foo name in the form.

如果要提交并保存各个复选框的值,则必须在字段命名中使用特定于PHP的数组符号:

If you want the individual checkbox values to be submitted AND saved, you'll have to use the PHP-specific array notation in the field naming:

<input type="text" name="foo[]" value="foo" />
<input type="text" name="foo[]" value="bar" />
<input type="text" name="foo[]" value="baz" />
                            ^^--note the [] brackets

这会将$_POST['foo']转换为数组,并告诉PHP将每个单独的值另存为数组元素.

This will turn $_POST['foo'] into an array, and tell PHP to save each individual value as an array element.

这篇关于如何通过多个复选框值通过PHP发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆