0

你好堆垛机,

PHP 新手,我正在从预先构建的代码中组合一个多页表单。

基本上,用户可以根据需要选择尽可能多的复选框......然后表单提交到这个辅助页面。这个二级页面回显了他们通过 .. 在页面顶部选择的复选框,$check然后他们可以输入他们的联系信息,所有信息都通过表单提交,连同$check信息。

除了$check没有输入表单消息外,一切都运行良好,但它在页面顶部运行,显示用户输入的选项。

任何帮助表示赞赏!

<?php
$emailOut = '';
if(!empty($_POST['choices'])) {
foreach($_POST['choices'] as $check) {
            echo $check; //echoes the value set in the HTML form for each checked checkbox.
                     //so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
                     //in your case, it would echo whatever $row['Report ID'] is equivalent to.
            $emailOut .= $check."\n"; //any output you want
}
}
$errors = '';
$myemail = 'test@myemailHERE.com';//<-----Put Your email address here.
if(empty($_POST['name'])  || 
   empty($_POST['email']) || 
   empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name']; 
$email_address = $_POST['email']; 
$message = $_POST['message']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail; 
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. $check ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message \n $emailOut"; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} 
?>
4

1 回答 1

0

在这种情况下,当您处理电子邮件时,$check会显示最后一个选项。您需要使用foreach语句来构建数组或电子邮件输出,例如

$emailOut = "";
foreach($_POST['choices'] as $check) {
        $emailOut .= $check."\n"; //any output you want
}

然后以相同的方式使用您的电子邮件变量

$email_body = "You have received a new message. Here are the details:\n Name: $name \n Email: $email_address \n Message \n     $message \n $emailOut";

更新

从进一步调查和提交的更多代码来看,您似乎正在处理多表单提交问题。问题是您有表单 1(复选框)提交到表单 2(电子邮件)。

因为在复选框提交后进行检查时,没有给出姓名、电子邮件等,所以$errors没有给出任何电子邮件。填写电子邮件表单时,复选框没有再次发送,$check甚至$_POST['choices']没有值。

您可以将两种形式合二为一,也可以通过传递值并填充“隐藏”字段来寻找一种方法来保存值(<input type='hidden' value='...'>)或使用 PHP.

于 2013-02-11T19:03:34.257 回答