8

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');
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83

6 Answers6

12
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";
}
Cosmin
  • 1,482
  • 12
  • 26
11

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.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
6

you can do this:

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

on database.php

echo $_SESSION['msg'];
unset($_SESSION['msg']);
Harshal
  • 3,562
  • 9
  • 36
  • 65
5

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"]);
    }
Renan Ferreira
  • 2,122
  • 3
  • 21
  • 30
3

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
  • 1
    I was wondering why in my implementation of this the messages weren't carried over after redirecting to another page. Turns out the `exit` command is very necessary in order for everything to work correctly. – Simon Josef Kok Mar 30 '16 at 21:43
  • when ever we are giving a header location,it should be followed by an exit statement.I think there may be some issues with the session.Have you started a session?? – Satyapriya Mishra Mar 31 '16 at 17:24
0

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.

bpatel
  • 381
  • 1
  • 4