繁体   English   中英

PHP重定向到带有警报对话框的页面

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM