How do I prevent a form from being reloaded while refreshing the browser page? [duplicate]

0

I have a form with the following structure below:

<?php
     //Lógica em php....
?>
<html>
.....
<form method="post" action="">
.....
<input type="submit">
.....

By clicking on the submit button the page reloads, executing on the server the php logic and returning the result to the page itself. However, when reloading the browser (F5 for example) the same question is asked if I want to reload the request. Is there a way to prevent the browser from forcing the client to always reload the last request?

Note: I know there are simple ways to solve this problem, such as using Ajax. But the idea here is to solve this problem without changing the current structure. Is this possible?

    
asked by anonymous 10.09.2018 / 13:06

2 answers

-2
Well, I'm going to suggest a simple way for you to resolve this without changing the structure, but I advise against any solution that maintains this structure because you're using business logic with your View, among other things. So next I'll suggest a better one, which will give you more freedom for future modifications, and it's not complicated.

Form 1) Start a session on the page, encapsulate all without code in an if (! $ _ SESSION ['...']), and after if start your session.

session_start();
if(!$_SESSION['pass']){ 
   ...
}
$_SESSION['pass'] = true;

The user will go through the IF block once, because the 'pass' session was not created, but it will not pass twice because it was created once it passed the block of code. You can vary this solution, add other validators, and so on.

Form 2) Remove all this PHP code and move it to a unique page (like a script) where you will only deal with PHP. Point the form to this script, and at the end of it, redirect the user to the form again. Still not the best way to do it but it is much more efficient than it is being done.

A hug and good luck.

    
10.09.2018 / 14:14
-3

This worked for me. In F5, the md5 of POST does not change. We used this loophole to identify whether the submit is real or just refresh.

  if( $_SERVER['REQUEST_METHOD']=='POST' )
    {
        $request = md5( implode( $_POST));

        if( isset( $_SESSION['last_request'] ) && $_SESSION['last_request'] 
== $request)
        {
        $caso = 'BEM_VINDO';
    }
    else
    {
    
10.09.2018 / 13:50