Display message after redirecting user to another web page

I am making 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 to show him the updated table. (I ended up with this part). At the same time, I want to display a message to the user on the original page (the one where the system has moved) to notify him of the success of the operation. How can I display this message?

Here is my php code that moves to another page.

Header( 'Location: Database.php'); 
+5
source share
6 answers
 Header( 'Location: Database.php?success=1' ); 

And on the Database.php page:

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

Save it in session as a sort of "flash" -message:

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

and show it in Database.php after the redirect. Also delete its contents after displaying it:

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

The advantage of this is that the message will not be displayed again each time the user refreshes the page.

+8
source

One solution is to put the message in SESSION in your php file. So, on the original page, you get this SESSION variable and display it. eg:

in your php file:

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

In the source file:

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

You can do it:

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

on database.php

 echo $_SESSION['msg']; unset($_SESSION['msg']); 
+3
source

The best way to solve this problem is to establish a session message after a successful operation on the process page. Then, on the redirected page, check if the session message is established or not. If it is installed, just an echo message. The code below 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 
+2
source

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

0
source

Source: https://habr.com/ru/post/1432100/


All Articles