0

Suppose there is a web form in your PHP page and it posts values by POST method to same page. Then you get values from $_POST and insert them to Mysql table. It is OK.

But when I want to feedback (success or error), I get confused.

I use bootstrap toast messages. For example, when input values are not regular or insert query fails, toast message says: Error: New record fails!

When insert query succeeds, toast message should say: Success!: New Record Added!

But visitor also can refresh the page, it means double time same code and double insert

To prevent resubmitting the page, I use header code. After successfull insert query, I redirect the page forexample like this:

header("location: page.php?NewRecord=Ok");

So I check by PHP code; is there any GET value in URL, and if there is "NewRecord=Ok", toast message says: Success!: New Record Added!

But, when visitor referesh the page, the URL does not change. It is still "page.php?NewRecord=Ok".

Thus, the visitor can still see same toast message by refreshing same page.

I think you understand me. What do you advice me?

mustklc
  • 11
  • 1
  • 1
    If your visitors choose to refresh the page that is kind of their issue, don't you think? Most people don't randomly refresh for no reason. – ADyson Oct 03 '21 at 16:09

1 Answers1

0

In my solution use session ($_SESSION).

It will save your notify to session and after refreshing it will delete self.

Use it like:


    //At saving to database:
    $_SESSION["notice"] = "saved";

   //after refresh (meaning somewhere on page where is  not button click needed):
   if (isset(t$_SESSION["notice"])) {
         unset($_SESSION["notice"];
         showmessage(); // replace with your message function
   }

Because unset function it will not appear many times but just once

Marty1452
  • 430
  • 5
  • 19