sasori
sasori

Reputation: 5463

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

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 ?

Upvotes: 18

Views: 78094

Answers (10)

hasan prakash
hasan prakash

Reputation: 1

after posting your details to session, write like this in value

<?php print $_SESSION['messenger'] ?>

if you are writing both php and HTML in same file then you can do like this

<?php print $messenger] ?>

Upvotes: 0

Ali Abdul
Ali Abdul

Reputation: 49

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!

Upvotes: 0

Rotem
Rotem

Reputation: 1379

There are some main issues to check in order to get the data appear after refresh or any event handler:

1) Be aware of the signature. it must contain the method="post" attribute as follows:

    <form method="post"/>

else the using of the $_POST['messenger'] will not work.

The default value of the method attribute in the form is get.

    <form>  equals to  <form method="get">

2) Many programmers mix between the attributes id vs name. Be aware of using the correct attribute. The $_POST associative array is using the name to get data from it. That is why the next example will not work correctly:

    <input type="text" id="data" value="<?php echo isset($_POST['data']) ? $_POST['data'] : "" )?/>"

To get it work right you must add the attribute name="data" as follows:

    <input type="text" name="data" id="data" value="<?php echo isset($_POST['data']) ? $_POST['data'] : "" )?/>"

Good luck

Upvotes: 0

T.Todua
T.Todua

Reputation: 56555

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

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

You can use autocomplete for a form too.

Upvotes: -1

arthurr
arthurr

Reputation: 1145

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

Upvotes: 7

WebStylePress
WebStylePress

Reputation: 1046

Session is a good way to store and remember data but why complicate things? Consider if the form has many fields, developer has to store lots of values in session. Why not simply print or echo.

Example:

Form is submitted with input ssName and hidden field ssAct with value of send

Get the ssName and ssAct on form submit

$ssAct=$_REQUEST["ssAct"];
$ssName=$_REQUEST["ssName"];

and the form

<input type="text" name="ssName" value="<?php if($ssAct=='send' && $ssName!='') { echo "$ssName"; } ?>">
<input type="hidden" name="ssAct" value="send">

On submit name will be echoed if it was submitted and was not empty.

Upvotes: 1

Khawer Zeshan
Khawer Zeshan

Reputation: 9646

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

Upvotes: 15

Igor Parra
Igor Parra

Reputation: 10348

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.

Upvotes: 0

Brad
Brad

Reputation: 163612

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.

Upvotes: 1

Chronial
Chronial

Reputation: 70883

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.

Upvotes: 2

Related Questions