简体   繁体   English

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

[英]displaying a message after redirecting the user to another web page

I am doing HTML with PHP and MySql. 我正在用PHP和MySql做HTML。 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? 如何显示此消息?

Here's my php code that moves to the other page. 这是我的php代码,可移至另一页。

Header( 'Location: Database.php');
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";
}

Store it in the session as sort of a "flash"-message: 将其作为“闪存”消息存储在session中:

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

and show it in Database.php after the redirect. 并在重定向后在Database.php显示它。 Also delete its content after displaying it: 在显示后还删除其内容:

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. 这样做的好处是,用户每次刷新页面时都不会再次显示该消息。

you can do this: 你可以这样做:

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

on database.php 在database.php上

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

One solution is to put the message in a SESSION, in your php file. 一种解决方案是将消息放入SESSION中的php文件中。 So in the original page, you get that SESSION variable, and display that. 因此,在原始页面中,您将获得该SESSION变量并进行显示。 ex: 例如:

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"]);
    }

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

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

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

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