How to keep input values when user goes to another page

Asked

Viewed 470 times

0

Well, I rode a form very simple to explain my problem.

<?php
$action = filter_input(INPUT_GET, 'action', FILTER_DEFAULT);

if ((!empty($action)) && ( $action === "add")) {
?>
    <script>
        alert('ERRO');
        window.history();
    </script>
    <?php
}
?>


<form action="form.php?action=add" method="post">
    <input type="text" name="nome">
    <input type="text" name="email">
    <input type="password" name="senha">
    <input type="submit" name="enviar" value="enviar" />
</form>

When there is an error in my form I send one alert for the user and it is redirected to the previous page to correct the error. The problem is that all form data is lost when it returns. Is there any way to resolve this? A way in which data is kept and only deleted when the form is correct?

  • this does not solve my problem, because in some input I already use the value to upload information.

  • If you understand how it works you will understand that you can solve yes. Regardless of different situations, the technique is the same. Note that you can still resolve with Ajax, Cookies or Sessions. Ajax may be more interesting because it does not redirect the page. Thus the data persists, in addition to saving data traffic.

  • What if instead of window.history you use window.Location? then add the fields that were inserted as query string... with that being they come you load in your form... something like window.Location= Document.referrer + '?index=1';

  • 1

    @h3nr1ke, the form action is leading to the same page.. No need to redirect. Just take the amounts of $_POST... The edition of the title defaced the context, giving rise to misinterpretation.

1 answer

1

Change window.history() for window.history.back() (equivalent to clicking the Back button of the browser), when you use the back button of the browser or even the function back browser itself recovers data from <form> (provided that the requisition has come from <form>)

<?php
$action = filter_input(INPUT_GET, 'action', FILTER_DEFAULT);

if ((!empty($action)) && ( $action === "add")) {
?>
    <script>
        alert('ERRO');
        window.history.back();
    </script>
    <?php
}
?>

<form action="form.php?action=add" method="post">
    <input type="text" name="nome">
    <input type="text" name="email">
    <input type="password" name="senha">
    <input type="submit" name="enviar" value="enviar" />
</form>

About the API history: https://developer.mozilla.org/en-US/docs/Web/API/History#Methods

  • worked well in parts, all fields were kept, but the password was deleted, as I keep it too?

  • @Hugoborges this is a matter of security, no browser keeps this data in the layer that we understand as "front-end", if notice in registration forms as of Gmail and Yahoo when it misses something almost every form is recovered, except for "password" and "repeat password" (both use <input type="password">), not great security, but it’s something standard input=password, I can even give you a hint, but only if you really think it’s something very necessary, if it’s a login form I wouldn’t recommend forcing it

  • I understand, good in the matter of <input type="password"> all right, the problem is that I use an auto-complete. And when the a something wrong its input is deleted, I wish that input was not erased.

  • But the option to autocomplete passwords is user action, the site can not have control over it @Hugoborges

  • use the auto complete for city place and not password. Ai when to something wrong the user has to inform the city again.

  • @Hugoborges is that you kind of changed the subject, this part of the autocomplete with city is also not in the body of the question, by the logic o . back should work, if you can show how this in a simple example I can test and see what it is possible to do.

  • good I think I better post another question, I’ll make an example and post ok.

  • @Hugoborges I don’t think it’s necessary, even if it’s a real problem I can even vote to reopen the question, because it looks different from the other one they marked here

Show 3 more comments

Browser other questions tagged

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