我的主页上有一个表单,提交时会运行包含以下代码的外部form.php文件。我正在测试MAMP并且标题重定向似乎没有工作,url只是卡在form.php网址上?我以前有一个回声功能,工作正常!?我究竟做错了什么?请提前帮助,非常感谢
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$robots = $_POST['robots'];
$from = 'From: Blah Register Form';
$to = 'sofi.smith@blah.com';
$subject = 'Blah Lead';
$body = "From: $name\n E-Mail: $email\n company: $company\n ";
if ($_POST['submit'] && $robots == '') {
if (mail ($to, $subject, $body, $from)) {
header("Location: http://google.com");
exit;
}
else {
echo '<p>Something went wrong, please try again</p>';
}
}
else if ($_POST['submit'] && $robots != '') {
echo 'Sorry, we don\'t like spammers here!';
}
?>
答案 0 :(得分:1)
我可以在第一个php标签之前看到一个空格,你也不应该关闭php标签。该空格意味着内容在标题之前发送。
但是,由于多种原因(标头已发送,警告等),您的标头语句可能会失败。一个快速的脏解决方法是使用javascript:
if (mail ($to, $subject, $body, $from)) {
echo '<script>location.href="http://www.google.com";</script>';
exit;
}
答案 1 :(得分:0)
首先编写error_reporting(E_ALL);
,然后您就可以在页面上看到确切的错误。
另外,我可以看到邮件功能,它应该是mail( $to , $subject , $message, $additional_headers )
我猜邮件功能有错误,这就是为什么标题功能不起作用
答案 2 :(得分:0)
希望这有效:
<?php
if($_POST) {
$name = mysql_real_escape_string(strip_tags($_POST['name']));
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$company = mysql_real_escape_string(strip_tags($_POST['company']));
$robots = mysql_real_escape_string(strip_tags($_POST['robots']));
$from = 'From: Blah Register Form';
$to = 'sofi.smith@blah.com';
$subject = 'Blah Lead';
$body = "From: $name\n E-Mail: $email\n company: $company\n ";
if($robots == '') {
$mail = mail($to, $subject, $body, $from);
if($mail) {
header('Location: http://google.com');
exit();
} else {
echo '<p>Something went wrong, please try again</p>';
}
} else if($robost != '') {
echo 'Sorry, we don\'t like spammers here!';
}
}
?>
答案 3 :(得分:0)
在两种情况下,您的条件并不总是解析为true。因此,除非您确定已发送电子邮件,否则您不知道是否正在调用任何代码。这有点紧张:
if($_POST['submit']) {
if ($robots == '') {
if (mail($to, $subject, $body, $from)) {
header("Location: http://google.com");
} else {
echo 'Something went wrong, please try again';
}
} else {
echo 'Sorry, we don\'t like spammers here!';
}
} else {
echo "submit was not set";
}
因为现在你有:
if ($_POST['submit'] && $robots == '') {
// send the email or whatever
} else if ($_POST['submit'] && $robots != '') {
echo 'Sorry, we don\'t like spammers here!';
}
表示如果未设置$_POST['submit']
,则两种情况都不成立。