好的,所以我发送一个html 5表单到一个带有一些隐藏字段的php脚本。一个隐藏的领域正在运作,但另一个不是。有人可以帮我清理一下吗?
以下是表格:
<form action="mail_action.php" method="post">
<!--Hidden information data-->
<input type="hidden" name="email_address" value="example@googlemail.com" />
<input type="hidden" name="email_subject" value="Hello World" />
<!--The form-->
<input name="form_data[]">
<input name="form_data[]">
<input name="form_data[]">
<input type="submit">
</form>
这是剧本:
<?php
//Grabbing hidden field data.
$email = $_POST["email_address"];
$subject = $_POST["email_subject"];
$headers = 'From: no-reply@example.co.uk' . "\r\n" .
'Reply-To: no-reply@example.co.uk' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($_POST['form_data'] as $item)
{
//Asigning the message fields to $message variable.
$message .= $item;
//The spaces inbetweeen
$message .= "\r\n";
}
if(mail ($email, $subject, $message, $headers)){
echo "Your Message was sucessfully emailed to: ".$email;
} else {
echo "Opps, Didn't send.";
}
?>
email_address隐藏字段有效,但不是主题,未来谢谢。
答案 0 :(得分:2)
从PHP手册:
(&#39;。=&#39;)是连接赋值运算符,它将右侧的参数附加到左侧的参数。
在你的脚本中,左侧的参数为空,它将生成一个E_NOTICE,表示$message
变量未定义。
换句话说,$message
变量不存在于循环之外,因此您将附加到不存在的变量。
这应该解决它:
$message = ""; //initialize it
foreach($_POST['form_data'] as $item)
{
//Asigning the message fields to $message variable.
$message .= $item;
...
}
答案 1 :(得分:1)
给出的代码运行良好,所有值都在$ _POST中填充。 检查一下,让我们知道结果
array(3) { ["email_address"]=> string(22) "example@googlemail.com" ["email_subject"]=> string(11) "Hello World" ["form_data"]=> array(3) { [0]=> string(5) "dwdcw" [1]=> string(2) "fw" [2]=> string(3) "dfw" } }