表单提交上的PHP重定向无效

时间:2018-04-20 16:11:02

标签: php redirect

我在使用基本联系表格时遇到了问题,而且我所阅读的所有内容似乎都没有帮助,我尝试了不同的方法,但没有真正有效。想法是在用户发送消息后,他被重定向到成功/错误页面。一切似乎都有效,表单有效 - 我收到了电子邮件,但它并没有将我重定向到请求的页面。任何建议将不胜感激!

以下是表单的代码。

    <div class="text-intro">

      <h1>Contact</h1><br><br>
      <p>To contact us please use the contact form visible. When sending files, please use the following e-mail</p><br><br><br><br><br>

      <form method="post" action="contactengine.php">

      <div class="contact-one">
        <p>
          <label for="Name">Username:</label><br><br>
          <input name="Name" id="Name" data-jkit="[validate:required=true;min=3;max=10;error=Please enter your username (3-10 characters)]">
        </p>
      </div>  

      <div class="contact-two">  
        <p>
          <label for="Email">E-mail:</label><br><br>
          <input name="Email" id="Email" data-jkit="[validate:required=true;strength=50;error=Please write your email]">
        </p>
      </div>  

      <div class="contact-three">  
        <p>
          <label for="Message">Message:</label><br><br>
          <textarea id="Message" name="Message"></textarea><br><br>

                  <input type="submit" name="submit" value="Submit" class="submit-button">
        </p>
      </div>            

        <p>
        </p>
      </form>


    </div>   

PHP:

<?php

$EmailFrom = "test@gmail.com";
$EmailTo = "test2@gmail.com";
$Subject = "New message";
$Name = Trim(stripslashes($_POST['Name'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Message = Trim(stripslashes($_POST['Message'])); 



// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
?>

1 个答案:

答案 0 :(得分:3)

我建议您不要打印HTML,而是修改标题以重定向浏览器。这是我在重定向时总是使用的。它的工作原理如下:

header('Location: http://www.example.com/');
exit;

确保在发送标题之前没有输出。