Home > Article > Backend Development > How to retain the filled-in information after PHP fails to submit the form_PHP Tutorial
This article will introduce to you some summary methods on how to retain the filled-in information after PHP fails to submit the form. The most commonly used method is to use caching. This method may cause problems if the network speed is slow. The best way is to use ajax. .
1. Use the header header to set the cache control header Cache-control.
PHP code
1.
The code is as follows | Copy code | ||||
header('Cache-control: private, must-revalidate'); //Support page bounce
| tr>
2. Use the session_cache_limiter method.
PHP code
The code is as follows | Copy code | ||||
1.session_cache_limiter('private, must-revalidate'); //Write before the session_start method
|
The following is an introduction to the session_cache_limiter parameters:
The meaning of several parameters in session_cache_limiter is:
nocache: Of course it is not cached (for example: the form information is cleared), but public variables can be cached
private: private mode cache (for example: form information is retained, but valid within the lifetime)
private_no_cache: private mode but does not expire (form information is retained)
publice: public mode, (form information is also retained Reserved)
Set cache expiration time: session_cache_expire function setting, the default is 180 minutes.
Commonly encountered problems:
1. session_cache_limiter("private"); The form information is retained, but if I modify the submitted information, the information presented on the form page is still the information in the cache, which cannot be automatically refreshed in time. If session_cache_limiter("private"); is not used, it cannot Retain form information
Solution:
Add
The code is as follows | Copy code | ||||
|
Another way we can use ajax to instance
index.html template file large content:
The code is as follows | Copy code |
|
There seems to be a bug in the original source code, but it runs normally after modification.
Contents of the login.php file:
The code is as follows
|
Copy code
|
||||
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$ _REQUEST['password'])); |
In this case, we don’t need to refresh the page when submitting. If it fails, there will be a submission directly, which is 100% The data will not be lost after the save and submit fails.
http: //www.bkjia.com/PHPjc/444583.html