如果注册成功,我需要从 php 显示一个警报对话框。我也想重定向到另一个页面。我的 php 代码在这里:
if($conn->query($sql)===TRUE){
echo "<script>alert('Registration Successfull');</script>";
header('Location: index.php');
exit();
}
但我收到一条警告消息:
Warning: Cannot modify header information - headers already sent by (output started at /home/bdacademichelp/public_html/medcino.com/signup_back.php:19) in /home/bdacademichelp/public_html/medcino.com/signup_back.php on line 20
第 19 行是显示警报消息的回显线
最佳答案
使用 JavaScript 警报会延迟重定向,直到用户单击“确定”。您可能想要做的是在成功订阅后在 index.php 中显示警报。有几种不同的方式来传递此信息,但最常见的方式是在 session 中传递成功消息。
//page1.php
<?php
session_start();
if($conn->query($sql)===TRUE){
$_SESSION['message'] = "Registration successful";
header('Location: index.php');
exit();
}
session_write_close ();
?>
//index.php
<?php
session_start();
if (isset($_SESSION['message'])) {
$show_message = $_SESSION['message'];
$_SESSION['message'] = null;
}
session_write_close ();
// ...
if (isset($show_message)) {
echo "<script>alert('{$show_message}');</script>";
}
其他替代方法是在 URL 中传递数据,例如 index.php?message=Registration%20Successful 或在 cookie 中传递消息。
关于javascript - php 重定向到带有警报对话框的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40815374/