简体   繁体   中英

displaying a message after redirecting the user to another web page

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?

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

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

And in the Database.php page :

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['message'] = 'success';

and show it in Database.php after the redirect. 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

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

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:

in your php file:

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中的成功消息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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