在我的表单提交并发送消息后,我想回到索引,但我也想在索引中显示回显"消息已发送".php在div 而不是我的进程中.php,我将如何做到这一点。如果你需要更多的代码,我会提供或链接到网站,这对 php 来说是新手。谢谢。
这就是我到目前为止尝试过的。在我的进程中.php文件
if(!$mail->send()) {
$output = 1;
// $output = 'Mailer Error: ' . $mail->ErrorInfo;
} else {
$output = 2;
header('Location:index.php');
}
在我的索引.php文件中
<?php
if ($output == 2) {
echo "<b>Message has been sent</b>";
} elseif ($output == 1) {
echo "<b>Message could not be sent, please try again</b>";
} else {}
?>
$output未在 index.php 文件中设置。
作为一种简单的开始方法,您可以像
header('Location:index.php?output='.$output);
并在索引中获取输出.php
$output = $_GET['output'];
在文件的开头,这样您就可以使 if 语句正常工作。
另请注意,如果进程中的 $output = 1,您将永远不会被重定向.php因为标头仅在 else 语句中。只需将标题放在 else 语句右括号之后即可。
if(!$mail->send()) {
$output = 1;
} else {
$output = 2;
}
header('Location:index.php?output='.$output);
die();
索引.php:
<?php
if (isset($_GET['output'])) {
$output = $_GET['output'];
if ($output == 2) {
echo "<b>Message has been sent</b>";
} elseif ($output == 1) {
echo "<b>Message could not be sent, please try again</b>";
}
}
请注意,您不应在生产环境中使用未经清理的请求数据(用户输入等),因为这是一种安全风险。
这行不通,值不会以这种方式传递给索引:
} else {
$output = 2;
header('Location:index.php');
}
您的选项(不包括重新设计其工作方式)是 POST 或 GET;
} else {
$output = 2;
header('Location:index.php?sent=1');
}
然后在索引中.php:
<?php
if (isset($_GET['sent']) && $_GET['sent'] == 1) {
echo "<b>Message has been sent</b>";
}
?>