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

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();
           }
       });

I had similar issue at one of my project so I wrote some js to handle this using cookies. First I found two simple functions to set and get cookies values:

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    } else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

Than I wrote two other functions to set and get input values:

function saveValue(input) {
    var name = input.attr('name');
    var value = input.val();
    setCookie(name,value);
}

function getValue(input) {
    var name = input.attr('name');
    var value = getCookie(name);
    if(value != null && value != "" ) {
        return value;
    }
}

And using jQuery I save user's input value to session cookie

$('input[type=text]').each(function(){
    var value = getValue($(this));
    $(this).val(value);
}).on('blur', function(){
    if($(this).val() != '' ) {
        saveValue($(this));
    }
});

I hope it'll help ;)

Tags:

Php