PHP重定向问题

PHP重定向问题,php,redirect,header,Php,Redirect,Header,提交表单后,我很难让我的页面重定向。我听从了迄今为止我能找到的建议,但没有运气。任何帮助都将不胜感激 <?php //If the form is submitted if(isset($_POST['submit'])) { $subject = "CONTACT FORM"; //Check to make sure that the name field is not empty if(trim($_POST['contactname']) == '') { $hasErr

提交表单后,我很难让我的页面重定向。我听从了迄今为止我能找到的建议,但没有运气。任何帮助都将不胜感激

<?php
//If the form is submitted
if(isset($_POST['submit'])) {

$subject = "CONTACT FORM";
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
    $hasError = true;
} else {
    $name = trim($_POST['contactname']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '')  {
    $hasError = true;
} else {
    $email = trim($_POST['email']);
}

//If there is no error, send the email
if(!isset($hasError)) {
    $emailTo = 'email@domain.com'; 
    $body = "Name: $name \nEmail: $email";
    $headers = 'From: Bond Limo (Newsletter Signup) <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    $emailSent = true;

    }
}
?>

  <?php if(isset($hasError)) { //If errors are found ?>
  <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
  <?php } ?>
  <?php if(isset($emailSent) && $emailSent == true) { 

    header("Location: http://www.website.com");

   } 
   ?>

  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="side-form">

请检查您是否用有效信息填写了所有字段。多谢各位


您的标题未被处理。为了处理标头并进行重定向,必须在标头之后调用
die()
函数。像这样:

<?php
    if(isset($emailSent) && $emailSent) {
        header("Location: http://www.website.com");
        die();
    }
?>

也许你可以从这里找到一些帮助:?可能重复的-1在标题之前不要做任何输出。它已经被问了很多次,请做更好的研究!谢谢你们。我只是不知道要搜索什么。为什么不
$emailSent=mail($emailTo,$subject,$body,$headers)谢谢Baraa H。我这样做了,但重定向仍然无法工作。页面的其余部分刚刚停止显示。有什么建议吗?嘿@Fishin'music,你能在问题中发布你的更新代码吗?然后我再帮你看看。
// If there is no error, send the email
if (!isset($hasError)) {
    $emailTo = 'email@domain.com'; 
    $body = "Name: $name \nEmail: $email";
    $headers = 'From: Bond Limo (Newsletter Signup) <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    $emailSent = mail($emailTo, $subject, $body, $headers);
} else {
    // Errors found
    <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
}