0

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

<!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 文件,然后该文件将通过电子邮件发送表单输出。我已经设法让所有其他字段成功发送,但如果没有添加功能(这使得总数)被中断,就无法让复选框值发送并通过电子邮件输出。

<?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
}
?>

请问有人有解决办法吗??无论我尝试什么,我都无法让复选框通过那里的值发送并像表格的其余部分一样在电子邮件中显示自己。

任何帮助将非常感激!

谢谢!

4

2 回答 2

1

您正在使用相同的名称构建您的复选框输入。这意味着 PHP 将处理表单提交并不断用新副本覆盖特定字段名称:值对的“先前”版本,例如

<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表单中名称的最后一个副本。

如果您希望提交并保存单个复选框值,则必须在字段命名中使用 PHP 特定的数组表示法:

<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 将每个单独的值保存为一个数组元素。

于 2013-10-21T16:04:23.540 回答
0
  <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

将复选框的名称更改为选择 [],以便您可以将其用作数组。

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

foreach($choice as $c){
   $emailbody .= 'Checkbox .'$c;
}

当您提交表单时,它将如下所示:

array(1) { ["choice"]=> array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> string(1) "4" [4]=> string(1) "5" } } 

因此,您可以像 $_POST['choice'][0] 或 [1] 或 [2] 一样访问它并继续。

于 2013-10-21T16:03:41.990 回答