如何让php重定向到新页面

时间:2016-12-17 14:33:07

标签: php html forms

所以我正在为某个人创建一个联系表单,我希望它在某些字段为空时给我一个错误消息。现在它的工作和东西,但每次都进入空白页面。我试过搜索,找不到答案。 我的PHP代码:

<?php
$from = $_POST['email_adress'];
$subject = $_POST['subject'];
$select = $_POST['select'];
$message2 = $_POST['message2'];
$name = $_POST['firstname'];
$name2 = $_POST['lastname'];
if ($_POST['firstname'] == "")
{
    echo "<script>
             alert('A First Name is Mandatory'); 
     </script>";
}
elseif  ($_POST['subject'] == ""){
    echo "<script>
             alert('A Subject is Mandatory'); 
     </script>";
}
elseif ($_POST['email_adress'] == "") {
    echo "<script>
             alert('An Email Adress is Mandatory'); 
     </script>";
}
elseif ($_POST['message2'] == "") {
    echo "<script>
             alert('A Message is Mandatory'); 
             <a href='contactus.html'>;
     </script>";

} else {
$email_subject = "A submittion form";
$to ="sudaiguy1@gmail.com";
$headers = "From: sudaiguy1@gmail.com";
$email_body = 'You have been contacted by $name $name2 and his email is $from. \n The message he wanted to say was in the general subject of $select and more specifically $subject and the message is $message2';
mail($to,$email_subject,$email_body, $headers);
}

&GT;

1 个答案:

答案 0 :(得分:1)

<?php header("Location: yourpage"); ?>

发送电子邮件后,您的网页上没有任何内容,因此您必须将用户重定向到包含某些内容的网页,或者在您的邮件发送代码之后添加内容。