After submitting the form, display the new values ​​inside the inputs instead of cached

ATTENTION: this is not a cache problem, not a server configuration and a browser problem. After a thorough study, I found that the problem is related to the problem in my code: Deep, my select request, which fills the fields, precedes my update request, forcing the form to always show values ​​before updating. After the reboot, of course, new updated values ​​will appear, because of which I will look in the wrong direction.

However, this question shows a good overview of all the possible solutions related to the caching problem.


I am creating an application on which there are forms filled with values ​​from a database. The user can change the input values ​​of the form. After sending (not via AJAX), the new values ​​are saved in the database. And again the same form is displayed, this time containing the new values ​​loaded directly from the database. However: my browser (Chrome v27.0.1453.116m on Windows 7) caches old values. New values ​​are displayed only when you redirect to my page.

<form id="edit_form" class="form" action="http://the.same.url/" method="post"> <input type="text" name="example" value="<?php echo $value_from_database; ?>" /> </form> 

I came across several solutions, none of which solved the problem:

  • Setting the autocomplete="off" attribute in a form tag: this does not seem to have an effect.
  • Setting the autocomplete="off" attribute for individual input tags: this also does not produce results, even in combination with the above solution
  • Resetting the form with JavaScript when loading the page: this gives some results, but apparently does not affect the radio buttons and others.
  • Preventing page caching through meta tags, as suggested here: Using <meta> tags to disable caching in all browsers? Also preventing caching via .htaccess or php headers has no effect.
  • Trying to cache by adding a random number to the action url as suggested below by comment by Miro Marcarian

Below is an overview of the proposed solutions: Make a page to tell the browser not to cache / save input values

What are my options? If possible, I would like to avoid asynchronously placing my form. It starts to look like I have no other choice. Any input is appreciated.

Please note that this behavior also appears in other browsers such as IE10.

The name of my page matches the value of one of the inputs in my form and also does not change when sending a new value, so we can determine that this is a caching problem.

The Google Chromes web developers plugin shows me the following headers:

 Pragma: no-cache Date: Sun, 30 Jun 2013 09:44:12 GMT Content-Encoding: gzip Vary: Accept-Encoding Server: Apache Transfer-Encoding: chunked Content-Type: text/html; charset=utf-8 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection: Keep-Alive Keep-Alive: timeout=15, max=100 Expires: Thu, 19 Nov 1981 08:52:00 GMT 200 OK 
+6
source share
5 answers

It feels like your server is sending 304: Not Modified instead of the data you usually send. This question may help you if you are using apache.

I think something like this should do (but it has not been verified). It should remove the If-Modified-Since header for each request that requests a php file, forcing the application to send content instead of Not-Modified status.

 <ifModule mod_headers.c> <FilesMatch \.php$> RequestHeader unset If-Modified-Since </FilesMatch> </ifModule> 
+1
source

You can change the attributes of the input name to a random string and save them in hidden input.

 <form id="edit_form" class="form" action="http://the.same.url/" method="post"> <input type="text" name="saejfb27982" value="..." /> <input type="text" name="iuqde37we83" value="..." /> <input type="hidden" name="associativeArray" value='{"example":"saejfb27982","example2":"iuqde37we83"}'> </form> 

(Since the name is different every time, the browser will not be able to match it with any form of caching, autocomplete ...)

+2
source

The first answer to this question suggests adding the following headers to disable browser caching:

 <?php header("Expires: Tue, 01 Jan 2000 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d MYH:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); ?> 
+1
source

I believe that you just forgot echo $value_from_database , which in turn reverts to the cached value of the browser by default. Instead, you should have the following input element:

 <input type="text" name="example" value="<?php echo $value_from_database; ?>"> 

While this should be correct $value_from_database , I'm not quite sure why you still couldn’t completely disable caching.

0
source

Could you please take:

 <form id="edit_form" class="form" action="http://the.same.url/" method="post"> <input type="text" name="example" value="<?php echo $value_from_database; ?>" /> </form> 

and change it to:

 value="<?php echo if(isset($_POST['example'])):$_POST['example']?$value_from_database; ?> 

What it says: See if the variable is already in the $_POST array after the form is $_POST . If yes, use this value; if not, pull from db.

You may need to set the session variables ( $_SESSION['ex'] = $_POST['example'] ) and make settings up to the code above

0
source

Source: https://habr.com/ru/post/948257/


All Articles