我是Php的新手,所以请温柔一点。
我写了一个网站,并添加了一个订阅时事通讯部分。用户在按下提交之前添加他们的姓名和电子邮件地址。我收到了电子邮件,所以大部分都在工作,但表格不会让我回到网站的主页。有人能指出我哪里错了吗?
提前感谢
网站上的代码是:
<form name="frmcontact" id="frmcontact" action="php/contact.php" method="post">
<input id="name" name="name" type="text" value="Name"
onblur="this.value=(this.value=='') ? 'Name' : this.value;" onfocus="this.value=(this.value=='Name') ? '' : this.value;" />
<input id="email" name="email" type="text" value="Email"
onblur="this.value=(this.value=='') ? 'Email' : this.value;" onfocus="this.value=(this.value=='Email') ? '' : this.value;" />
<input name="submit" id="send" type="submit" value="Message" />
</form>
The code in contact.php is:
<?php session_start();
if(!$_POST) exit;
$address = 'email@emailaddress.com';
$email_subject ="Add me to the mailing list";
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = "Please add me to the mailing list ";
if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
$e_subject = 'You''ve been contacted by ' . $name . '.';
$msg = "You have been contacted by $name with regards to $subject.'r'n'n";
$msg .= "$message'r'n'n";
$msg .= "You can contact $name via email, $email.'r'n'n";
$msg .= "-------------------------------------------------------------------------------------------'r'n";
if(@mail($address, $subject, $msg, "From: $email'r'nReturn-Path: $email'r'n"))
{echo "<p class='ajax_success'>Thanks for Contact Us.</p>"; }
else
{echo "<p class='ajax_failure'>Sorry, Try again Later.</p>"; }
header('Location:http://www.websiteaddress/homepage.html');
?>
删除header
之前的所有输出使用header
之后的exit
,并考虑在下方标记空格
v
header('Location: http://www.websiteaddress/homepage.html');
exit;
使用正确的目录路径
if(isset($_POST['submit'])){
$address = 'email@emailaddress.com';
$email_subject ="Add me to the mailing list";
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = "Please add me to the mailing list ";
if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
$e_subject = 'You''ve been contacted by ' . $name . '.';
$msg = "You have been contacted by $name with regards to $subject.'r'n'n";
$msg .= "$message'r'n'n";
$msg .= "You can contact $name via email, $email.'r'n'n";
$msg .= "-------------------------------------------------------------------------------------------'r'n";
if(@mail($address, $subject, $msg, "From: $email'r'nReturn-Path: $email'r'n"))
{echo "<p class='ajax_success'>Thanks for Contact Us.</p>"; }
else
{echo "<p class='ajax_failure'>Sorry, Try again Later.</p>"; }
header('Location: homepage.html');
}
在使用header()之前不能输出任何内容
HTTP标头应在服务器的任何输出之前发送。如果你以前有输出,服务器会输出一个警告,比如"标头已经发送"
<?php
ob_start();
echo "redirecting now ....";
header('Location: http://www.websiteaddress/homepage.html');
exit();
ob_end_flush();
?>