PHP多形式故障

时间:2013-02-11 18:45:20

标签: php html forms

Hello Fellow Stackers,

PHP新手,我正在整理预先构建的代码中的多页表单。

用户基本上会根据需要选择多个复选框...然后表单会提交到此辅助页面。这个辅助页面回显是他们通过$check在页面顶部选择的复选框..然后他们可以输入他们的联系信息,所有信息都通过表单提交,以及$check信息。< / p>

除了$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');
} 
?>

1 个答案:

答案 0 :(得分: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会话来查看保存值的方法。