简体   繁体   English

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

[英]php redirecting to a page with alert dialog

I need to show a alert dialog from php if registration successfull. 如果注册成功,我需要显示一个来自php的警报对话框。 As well as I want to redirect to another page. 以及我想重定向到另一个页面。 my php code is here: 我的PHP代码在这里:

 if($conn->query($sql)===TRUE){
            echo "<script>alert('Registration Successfull');</script>";
            header('Location: index.php');
            exit();
    }

but I get an warning message which says: 但我收到一条警告消息,内容为:

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 警告:无法修改标头信息-已在第20行/home/bdacademichelp/public_html/medcino.com/signup_back.php中发送的标头(输出始于/home/bdacademichelp/public_html/medcino.com/signup_back.php:19)。

line 19 is the echo line which shows the alert message 第19行是显示警报消息的回显行

you can print message in alert and redirect as below: 您可以在警报中打印消息并按如下所示重定向:

echo "<script>alert('Success');document.location='index.php'</script>";

In your code you already send something as echo so you cannot use header after that. 在您的代码中,您已经发送了一些东西作为回显,因此您之后不能再使用header。

Using a javascript alert is going to delay the redirect until the user clicks OK. 使用JavaScript警报会延迟重定向,直到用户单击“确定”为止。 What you probably want to do is display the alert in your index.php when there has been a successful subscription. 您可能想要做的是在成功订阅后在index.php中显示警报。 There are a few different ways of passing this information but the most common way is to pass the success message in the session. 传递此信息的方式有几种,但是最常见的方式是在会话中传递成功消息。

// page1.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 // 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>";
}

Other alternatives are to pass the data in the URL such as index.php?message=Registration%20Successful or passing the message in a cookie. 其他替代方法是在URL中传递数据,例如index.php?message = Registration%20Successful或在cookie中传递消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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