4

I'm following the Post/Redirect/Pattern for a simple form in Symfony2. This form contains an id text field, which is dynamically populated in the controller with a random value.

I've noticed some odd behaviour in Chrome - if the user submits the form and then clicks back, the id field contains a brand new value. If I edit this id and then repeat the process, the value does get cached, so it looks like Chrome only bothers caching it if it sees that the value has changed.

This behaviour doesn't occur in Firefox or Safari. Is there any way to get Chrome to perform the same way? An answer in this question says that the issue lies with using a hidden field, but as I'm just using a standard text field I'm at a loss.

Community
  • 1
  • 1
Jonathan
  • 13,947
  • 17
  • 94
  • 123

3 Answers3

4

As far as I know there is no standards that specify what should happen here. So you can't really rely on the browsers behavior.

So, if it is really important for you that it should be new id when the users press the back-button, then make it happen with some client side script. No matter what browser is being used.

Tobias Nyholm
  • 1,142
  • 9
  • 16
  • I was trying to avoid using client side script as I want to support users without JS enabled, but it looks like this may be the only option. Thanks for your answer. – Jonathan Jan 13 '15 at 19:56
  • @Jonathan - Are you aware of what percentage of your site visitors have JS disabled? – Spencer Ruport Jan 14 '15 at 00:40
3

I suggest you to use Local Storage for this problem.

<input id="some_id">

<script type="text/javascript">

   localStorage.setItem("ID", "Some Id Value");

   if(! localStorage.getItem("ID"))
     localStorage.setItem("ID", "Some Id Value"); `

     $("#some_id").val(localStorage.getItem("ID"));
</script>

You can check local storage availabe or not..

if( typeof(Storage) !== "undefined" ) {
 // Your Browser support Local Storage
} else {
 // Your Browser does not support Local Storage
}

and track with Chrome's console. I cant upload image for this. Open Chrome's console,

Resource-> Local Storage -> Your Sitename

Tigin
  • 391
  • 5
  • 19
2

Most modern browsers cache form input values. So when user refresh page, the inputs have the same values. So, browser get html and make input with that value.

<input value="SOME_VALUE">

But, if user modified that field browser will use user's value instead of html's value. I suggest you to add autocomplete="off" to your id field.

Alexey B.
  • 11,965
  • 2
  • 49
  • 73
  • Correct. But I'm not looking to turn autocomplete off - in fact I'm asking the opposite. – Jonathan Jan 10 '15 at 11:03
  • I think this is the only way to get the same behavior on all browsers. This is not what you want but, on my opinion, what you should do. And then you can rely on a cross-browser behavior... – AlterPHP Jan 13 '15 at 14:45