how to remember input data in the forms even after refresh page?

后端 未结 10 1441
日久生厌
日久生厌 2020-11-27 17:11

what to do in order to make the form remember the previous input or the current input of the user even after he/she refreshed the page ?

should I do ,



        
相关标签:
10条回答
  • 2020-11-27 17:35

    You could also do without session like the below code:

    <input type="text" name="Fname" value="<?php if(isset($_POST['Fname'])){ echo $_POST['Fname']; } ?>" /><br/>
    

    This will keep the typed data inside the input box!

    0 讨论(0)
  • 2020-11-27 17:37

    That quite depends on where the data in the fields is coming from. If your scenario is that you are presenting a blank form, the user enters some data and then without submitting it, refreshes the page, there is nothing you can do. That's up for the browser to handle.

    You might be able to do some very weird JavaScript hacking there, but I would not suggest that.

    On the other hand, if your data is coming from a previous page or something like that, that's a different story. But then you need to tell us where the data is coming from, so I can give you a reasonable answer.

    0 讨论(0)
  • 2020-11-27 17:39

    on the page where your form is submitting do something like this

        session_start();
        $_SESSION['data'] = $_POST['data'];
        $_SESSION['data_another'] = $_POST['data_another'];
    

    and than you can access those session variables any where like this

        session_start(); // this should be at the top of the page before any html load
        <input type="text" name="name" value="<?php echo $_SESSION['data'];?>"/>
    

    refresh your page on success call like this

         $.ajax({
               type: "POST",
               url: "yourfile.php",
               data: 'data='+ data,
               success: function(){
                   location.reload();
               }
           });
    
    0 讨论(0)
  • 2020-11-27 17:44

    If you mean, filling out form fields and clicking refresh, then there is no direct way, as the data isn't submitted. That sort of behavior is up to the browser.

    The only thing you could do is post the data via AJAX back to your server as the fields change. Your PHP script receiving the data would set some session variables. When the form loads, you would set their default values to the appropriate session values.

    Rarely though is there a need for this kind of behavior. There will be many requests sent back and forth to your server.

    0 讨论(0)
  • 2020-11-27 17:44

    try the attribute autocomplete, maybe it will work on some browsers...

    <input type="text" autocomplete="on" />
    

    You can use autocomplete for a form too.

    0 讨论(0)
  • 2020-11-27 17:49

    None of your suggestions will help you "remember" data after a refresh (action that not send data to the server). What you need is to grab data via javascript (maybe with onkeyup events type) and grab it using cookies. With jquery is very easy.

    0 讨论(0)
提交回复
热议问题