How to prevent a form from being reloaded when updating your browser page?

Asked

Viewed 1,691 times

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 the Ubmit button the page reloads, running php logic on the server and returning the result to the page itself. However, when reloading the browser (F5 for example) the same question is asked if you want to reload the request. There is a way to prevent the browser from forcing the client to always reload the last request?

Note: I know that 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. This is possible?

  • I didn’t quite understand your question, but if you want to send the form data without updating the page you can do the form Submit in an iframe using target, or use the Jquery ajax, if you want to "keep the form data" while updating the page you can save itlos em cookies em javascript ou em SESSION no servidor(ai vai depende da linguagem que servidor está usando)

2 answers

-2

You see, I’m going to suggest a simple way for you to solve this without altering the structure, but I would advise against any solution that maintains this structure, because you’re using business logic with your View, among other details. So next I’m going to suggest you a better one, which will give you more freedom from future modifications, and it’s not complicated.

Form 1) Sign in to the page, encapsulate all without code in an if(! $_SESSION['...']), and after if starts your Session.

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

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

Form 2) Remove all this PHP code and move it to a unique page (like a script) where it will only handle PHP. Point the form to this script, and at the end of it redirect the user to the form again. It’s not the best way to do it yet but it’s much more efficient than how it’s being done.

A hug and good luck.

  • The problem of method 1 is that this would permanently block the execution of logic. E.g.: Let’s say the user runs the form. When reloading the page the logic will not be executed. But since this control is being done by one session, if it goes to another page and goes back to it, the logic will not run again unless I do a new check on other pages of the project.

  • Method 2 would not work because it changes the rule of not changing the structure. Remembering that this issue will not solve a real problem. I brought it to Stahl Overflow as a form of debate. Sometimes we can find situations like it is in legacy systems. Imagine a 30,000-line logic. In this case, an alternative might be better that separates the files as it will bring less impact to the legacy code.

  • Ah, okay, I figured that was your intention, to perform only once.

-3

This worked for me. In F5, the POST md5 does not change. So we use this gap 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
    {
  • I will test this solution

  • What if you need to submit the same information a second time, not by updating the page? That’s gonna block it from being done, not to mention it’s a scam. Ideally an HTTP response to the post should not have an HTML body, but redirect with the header Location for the new resource created.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.