如果注册成功,我需要从php显示一个警告对话框。 我想要重定向到另一个页面。 我的PHP代码在这里:
if($conn->query($sql)===TRUE){
echo"alert('Registration Successfull');";
header('Location: index.php');
exit();
}
但我收到一条警告信息:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/bdacademichelp/public_html//signup_back.php:19) in
/home/bdacademichelp/public_html//signup_back.php on line
20
第19行是显示警报消息的回波线
使用javascript警报会延迟重定向,直到用户单击"确定"。 您可能想要做的是在订阅成功时在index.php中显示警报。 传递此信息有几种不同的方法,但最常见的方法是在会话中传递成功消息。
// page1.php
session_start();
if($conn->query($sql)===TRUE){
$_SESSION['message'] ="Registration successful";
header('Location: index.php');
exit();
}
session_write_close ();
?>
// index.php
session_start();
if (isset($_SESSION['message'])) {
$show_message = $_SESSION['message'];
$_SESSION['message'] = null;
}
session_write_close ();
// ...
if (isset($show_message)) {
echo"alert('{$show_message}');";
}
其他替代方法是传递URL中的数据,例如index.php?message = Registration%20Successful或在cookie中传递消息。
您可以在警报和重定向中打印消息,如下所示:
echo"alert('Success');document.location='index.php'";
在你的代码中,你已经发送了一些东西作为回声,所以你不能在那之后使用标题。
现在它有效。 谢谢
如果有用,给它真实。