Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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 ,

            <div class="row">
             <label>Contact Messenger </label>
             <input type="text" name="messenger" id="messenger" size="50" maxlength="50" value="<?php echo $_SESSION['messenger']; ?>"/>
            </div>

or

            <div class="row">
             <label>Contact Messenger </label>
             <input type="text" name="messenger" id="messenger" size="50" maxlength="50" value="<?php echo $_POST['messenger']; ?>"/>
            </div>

or

there's a trick to do it ?..how ?

like image 989
sasori Avatar asked Aug 10 '11 16:08

sasori


People also ask

How do you retain a value after refresh?

We can retain form components values after the page refreshes or reloads by using JavaScript and PHP. Here we are using JavaScript to format the query string which will be used at address bar at the time of page reload.

How do you retain the values of form after submit in JavaScript?

To keep the values, you must fill in the values on the server while rendering the page. Usually, you can simply copy the data from the HTML request parameters into the fields. Usually, you cannot simply copy the HTML request parameters into the fields.


1 Answers

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();
           }
       });
like image 92
Khawer Zeshan Avatar answered Sep 21 '22 13:09

Khawer Zeshan