How to save web form contents for back button

When a web form submits and transfers the user to another page, it often happens that the user clicks the "Back" button to submit the form again (the form is an advanced search in my case).

How can I reliably save the form parameters selected by the user when they click "Back" (so they don’t need to start from scratch, filling out the form again if they change only one of the many form elements?)

Do I need to go the way of storing form parameters in session data (cookies or server side) or is there a way to get the browser to handle this for me?

(the environment is PHP / JavaScript - and the site should run on IE6 + and Firefox2 +)

+5
source share
8 answers

I would put him in a session.
This will be the most reliable, and even if they do not go back, he will still have his own search options. Including it in a cookie will also work, but is not recommended if it is not very small.

+4
source

I believe that you are in the grip of the browser. When you hit back, the browser does not make a new request to the server for content, it uses a cache (in almost every browser that I saw anyway). So nothing server-side is working.

, - , , cookie onunload , cookie javascript - , , .

+6

, .

IE, Firefox .. , ""... , , , no-cache script.

( , . , - .)

+3

, , , , .

AJAX .

+2

(, , ) POST. Javascript , AJAX ( JSON, , JSON) .

jQuery:

$( document ).ready( function() {
  $.getJSON(
    "/getformdata.php",
    function( data ) {
      $.each( data.items, function(i,item) {
        $( '#' + item.eid ).val( item.val );
      } );
    });
} );

/getformdata.php , :

{
  'items': [
    {
      'eid': 'formfield1',
      'val': 'John',
    },
    {
      'eid': 'formfield2',
      'val': 'Doe',
    }
  ]
}

, , , . , .

: Opera Firefox . JS- , .

+1

ajaxy ( javascript) , javascript, ( , ). , "".

0

cookie ? , , . , , , cookie , .

0

javascript. , cookie , pageload , cookie. - :

$('#formSubmitButton').click( function() {
    var value = $('#searchbox').val();
    var days = 1;
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = "searchbox="+value+expires+"; path=/";
});

$(document).ready( function() {
    var nameEQ = "searchbox=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) var valueC = c.substring(nameEQ.length,c.length);
   }
   $('#searchbox').val(valueC);
});

, . jQuery.

cookie : http://www.quirksmode.org/js/cookies.html

, FireFox IE .

0

All Articles