Javascript 将用户重定向到另一个网页后显示一条消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12249906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:31:27  来源:igfitidea点击:

displaying a message after redirecting the user to another web page

phpjavascripthtml

提问by Traveling Salesman

I am doing HTML with PHP and MySql. After some database operation that was performed by the user, my system redirects the user to the original database page in order to show him the updated table. (I am done with this part). At the same time, I wish to display a message to the user on the original page (the one where the system moved) to notify him with the success of the operation. How can I possibly display this message?

我正在用 PHP 和 MySql 做 HTML。在用户执行了一些数据库操作之后,我的系统将用户重定向到原始数据库页面,以便向他显示更新后的表。(我已经完成了这部分)。同时,我希望在原始页面(系统移动的页面)上向用户显示一条消息,通知他操作成功。我怎么可能显示这条消息?

Here's my php code that moves to the other page.

这是我移动到另一个页面的 php 代码。

Header( 'Location: Database.php');

回答by Cosmin

Header( 'Location: Database.php?success=1' );

And in the Database.php page :

在 Database.php 页面中:

if ( isset($_GET['success']) && $_GET['success'] == 1 )
{
     // treat the succes case ex:
     echo "Success";
}

回答by insertusernamehere

Store it in the sessionas sort of a "flash"-message:

将它存储在session某种“闪光”消息中:

$_SESSION['message'] = 'success';

and show it in Database.phpafter the redirect. Also delete its content after displaying it:

Database.php在重定向后显示。显示后也删除其内容:

print $_SESSION['message'];
$_SESSION['message'] = null;

The advantage of this is, that the message won't be shown again every time the user refreshes the page.

这样做的好处是,每次用户刷新页面时都不会再次显示该消息。

回答by Harshal Mahajan

you can do this:

你可以这样做:

$_SESSION['msg']="Updation successfully completed";
header("location:database.php");

on database.php

在数据库.php

echo $_SESSION['msg'];
unset($_SESSION['msg']);

回答by Renan Ferreira

One solution is to put the message in a SESSION, in your php file. So in the original page, you get that SESSION variable, and display that. ex:

一种解决方案是将消息放在 php 文件中的 SESSION 中。因此,在原始页面中,您会获得 SESSION 变量并显示它。前任:

in your php file:

在你的 php 文件中:

session_start();
$_SESSION["message"]="MESSAGE OF SUCCESS"

In you original file:

在你的原始文件中:

session_start();
    if(isset($_SESSION["message"]))
    {
        echo"SUCCESS OR THE MESSAGE SET IN THE VAR SESSION";
        unset($_SESSION["message"]);
    }

回答by Satyapriya Mishra

The best way to solve this problem is set a session message after the success of your operation in the process page. Then in the redirected page check whether the session message is set or not.If it is set then simply echo that message. The below code may help you.

解决这个问题最好的办法就是在你的流程页面中设置你操作成功后的会话消息。然后在重定向页面中检查会话消息是否已设置。如果已设置,则只需回显该消息。下面的代码可能对你有帮助。

 $_SESSION['MSG']="Your data is saved";
 Header( 'Location: Database.php');
 exit;
 //now in the database.php page write at the top
  <?php
   if(isset($_SESSION['MSG'])){
   echo $_SESSION['MSG'];
   }
  ?>//its very simple,you can also format the message  by using different html attributes

回答by bpatel

Before redirecting to new page, you can set a cookie with the message you want to show, upon loading the original page you will see if this special cookie is set and if it is you can display the success message stored in the cookie.

在重定向到新页面之前,您可以使用要显示的消息设置 cookie,在加载原始页面时,您将看到是否设置了此特殊 cookie,如果是,您可以显示存储在 cookie 中的成功消息。