【发布时间】:2012-04-05 23:26:49
【问题描述】:
我有一个表格,我想用它来收集用户信息。我有一个获取信息并存储在变量中的 php。当用户单击提交按钮时,表单 gest 提交并成功重定向到感谢页面。但是由于某种原因,我没有按预期收到电子邮件。有人可以解释我在哪里做错了。
以下是我的 HTML 和 PHP 文件。稍后我将添加 javascript 验证。此时我只想让表单提交工作。
PHP
<?php
//This page should not be accessed directly. Need to submit the form.
if(!isset($_POST['submit'])){
echo "ERROR. This page cannot be accessed directly";
}
//Variables from the form
$name = $_POST['fullname'];
$ageGroup = $_POST['ageGroup'];
$gender = $_POST['gender'];
$visit = $_POST['visit'];
$purposeOfVisit = $_POST['purposeOfVisit'];
$otherReferrer = $_POST['otherReferrer'];
$member = $_POST['member'];
$socialMedia = $_POST['socialMedia'];
$difficulties = $_POST['difficulties'];
$easeOfUse = $_POST['easeOfUse'];
$whatDifficulties = $_POST['whatDifficulties'];
$specialCondition = $_POST['specialCondition'];
$browser = $_POST['browser'];
$preferredFormat = $_POST['preferredFormat'];
$contentsForWebsite = $_POST['contentsForWebsite'];
$oldContents = $_POST['oldContents'];
$expectations = $_POST['expectations'];
$message = "Fullname:"; // will compose later
// Compose email
$email_from = "subash.adikari@gmail.com";
$email_subject = "Infinity User Questionnaire";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$to = "subash.adhikari@gmail.com";
$headers = "From: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thankyou.html');
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
如果需要,可以下载 index.php here。
非常感谢。
【问题讨论】:
-
该网站是在实时网站上还是在您的本地主机上?
-
在您确定
$_POST中没有任何内容后,您不会阻止代码加载。您只需添加一条警告消息。始终启用 error_reporting。 -
你能定义
not as expected吗? -
@Starx 我期待发送电子邮件。但是我在提交表单时没有收到任何电子邮件。
-
请注意,在 Windows 主机上,PHP 的内置 mail() 函数很可能会失败,因为我在 WS 主机上遇到了各种原因......又是一堆 MS 错误。跨度>
标签: php html webforms html-email