为什么我的表单没有重定向到标题中的链接?



我到处寻找解决方案,但没有找到。我的网站上有一个表单,他们在localhost上工作得很好,但在我的网站服务器上托管时工作得很难。当用户单击提交时,如果字段完全完成,则发送电子邮件,但不会重定向到标题上提到的链接("位置:"(。在适当的位置转到页面";谢谢.php";,页面";contact.php";就像在空版本中重新加载一样(全白屏幕(。

你能帮我找到解决办法吗?非常感谢!

之前的PHP代码

<?PHP
// form handler
function validateFeedbackForm($arr)
{
extract($arr);
if(!isset($name, $email, $subject, $message)) return;
if(!$name) {
return "DON'T PANIC! It seems, there are an error with the name.";
}
if(!preg_match("/^S+@S+$/", $email)) {
return "DON'T PANIC! It seems, there are an error with the email address.";
}
if(!$subject) $subject = "Contact from website";
if(!$message) {
return "DON'T PANIC! It seems, there are an error with the message.";
}
// send email and redirect
$to = "email@domain.com";
$headers = $subject. " - " .$name. " " .$email. "rn" .
'Reply-To: ' .$email . "rn" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
header('Location: ./thanks.php'); 
exit;
}
// execution starts here
if(isset($_POST['sendfeedback'])) {
// call form handler
$errorMsg = validateFeedbackForm($_POST);
}
?>

表单代码

<form method="POST" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" accept-charset="UTF-8">
<?PHP
if(isset($errorMsg) && $errorMsg) {
echo "<p id="error-msg">",htmlspecialchars($errorMsg),"</p>nn";
}
?>
<input placeholder="Your name *" class="input-class ct-yourname" type="text" size="48" name="name" value="<?PHP if(isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>">
<input placeholder="Your email address *" class="input-class ct-email" type="email" size="48" name="email" value="<?PHP if(isset($_POST['email'])) echo htmlspecialchars($_POST['email']); ?>">
<input placeholder="Subject of your message" class="input-class ct-subject" type="text" size="48" name="subject" value="<?PHP if(isset($_POST['subject'])) echo htmlspecialchars($_POST['subject']); ?>">
<textarea placeholder="Hey, ... *" class="input-message ct-message" name="message" cols="48" rows="8"><?PHP if(isset($_POST['message'])) echo htmlspecialchars($_POST['message']); ?></textarea>
<input class="btn-submit" type="submit" name="sendfeedback" value="Send Message">
</form>

可能的原因:

  1. 在调用导致问题的header()之前生成了一些输出。确保在调用header()之前没有生成任何输出。

  2. 确保包含表单的文件是utf-8编码的,而不是其他文件。

  3. 运行phpinfo()并确保output_bufferingnot empty

解决方案:您可以在脚本中的PHP打开标记<?php之后添加ob_start();,如下所示:

<?php
ob_start();
// other lines

感谢OMi Shah让我找到了一个很好的解决方案。在header()之前,当我查看代码时,没有为我生成任何内容。。。但事实上,这并不完全是真的。

我在PHP代码块之前得到了一小段代码:<!DOCTYPE php>。这一行只是none重定向的原因(实际上是同一页上的循环(。如果其他初学者也遇到了同样的问题,不要忘记在PHP代码之前不要放任何代码。

谢谢&小心!

最新更新