【问题标题】:Php show error or ok message before redirect重定向前 PHP 显示错误或 ok 消息
【发布时间】:2014-05-02 13:56:25
【问题描述】:

通过表单发送电子邮件后,我想发送一条消息,例如“邮件已发送”或“错误,重试!”在将用户重定向到另一个页面之前。这是我在发送电子邮件后使用的 php 代码:

<?php
[... send mail php code]
$mail->MsgHTML($_POST["message"]);
// this messages are not show, of course
if (!$mail->send()) {
echo '<script language="javascript">';
echo 'alert("Mailer Error: " . $mail->ErrorInfo;)';
echo '</script>';

} else {
echo '<script language="javascript">';
 echo 'alert("Message successfully sent. Thank\'s")';
echo '</script>';

}

header( 'Location: http://localhost:8888/contacts.html' ) ;

?>

我该怎么做?

【问题讨论】:

标签: php


【解决方案1】:

试试这个:

<?php
// Mail code goes here

echo 'Successfull';
header( "refresh:5; url=Location: http://localhost:8888/contacts.html" );
?>

【讨论】:

  • 您不知道它是否会起作用,因为您从问题 cmets 中链接的答案中复制了它?
  • 不,我有一点 PHP 经验。我知道它会起作用。它会回显“Successfull”,然后在 5 秒后刷新。
  • 您不需要header 中的Location: 位。
  • php header 必须在 echo 之前完成,否则它不起作用
【解决方案2】:

你可以这样做:

header( "refresh:5; url=http://localhost:8888/contacts.html" );

if(!$mail->send($to, $subject, $body)) {
   echo "Didn't send yo";
} else {
  echo "Sent";
}

或者这个:

if(!$mail->send($to, $subject, $body)) {
   echo "Didn't send yo";
} else {
  echo "Sent";
}

sleep(NUMBER_OF_SECONDS);
@header( 'Location: http://localhost:8888/contacts.html' ); //Please note that this method is a big no!!

或者如果 PHP 不适合你:

echo "<script>window.setTimeout(function() {window.location.href = 'http://localhost:8888/contacts.html';},NUMBER_OF_SECONDS);</script>";

【讨论】:

  • @JohnBupit 不确定,但您可以在 header (@header) 的前面加上 @。通常是一个很大的不,但它有效。
  • 您始终可以使用第二个选项并将header 放在顶部。 @JohnBupit
  • 我认为@ 只是为了抑制错误。
  • 是的,实际上并没有修复它,所以就像我说的,一个很大的不。 @JohnBupit
猜你喜欢
  • 1970-01-01
  • 2017-07-06
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-24
相关资源
最近更新 更多