[英]php redirecting to a page with alert dialog
如果注冊成功,我需要顯示一個來自php的警報對話框。 以及我想重定向到另一個頁面。 我的PHP代碼在這里:
if($conn->query($sql)===TRUE){
echo "<script>alert('Registration Successfull');</script>";
header('Location: index.php');
exit();
}
但我收到一條警告消息,內容為:
警告:無法修改標頭信息-已在第20行/home/bdacademichelp/public_html/medcino.com/signup_back.php中發送的標頭(輸出始於/home/bdacademichelp/public_html/medcino.com/signup_back.php:19)。
第19行是顯示警報消息的回顯行
您可以在警報中打印消息並按如下所示重定向:
echo "<script>alert('Success');document.location='index.php'</script>";
在您的代碼中,您已經發送了一些東西作為回顯,因此您之后不能再使用header。
使用JavaScript警報會延遲重定向,直到用戶單擊“確定”為止。 您可能想要做的是在成功訂閱后在index.php中顯示警報。 傳遞此信息的方式有幾種,但是最常見的方式是在會話中傳遞成功消息。
// 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中傳遞消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.