How to keep the information you have filled out after PHP fails to submit a form _php tips

Source: Internet
Author: User

This article describes how the PHP submission form failed to retain the information to fill out some of the methods summarized, the most common is the use of caching, this method if the slow speed of the network may be a problem, the best way is to use Ajax.

1. Use header header to set cache control header Cache-control.

The PHP code is as follows:

Header (' Cache-control:private, Must-revalidate '); Support page Bounce 

2. Use the Session_cache_limiter method.

The PHP code is as follows:

Session_cache_limiter (' Private, must-revalidate '); To write before the Session_Start method 

Here are 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: Proprietary caching (for example: form information is retained but valid for the lifetime)
Private_no_cache: Private mode but not expired (form information is retained)
Publice: Public Way, (form information is also retained)
Set cache Expiration Time: Session_cache_expire function setting, default is 180 minutes.

Often encounter problems:

1.session_cache_limiter ("private"); The form information is retained, but if I modify the information already submitted, the information presented by the form page or the information in the cache is not automatically refreshed in time, if not session_cache_ Limiter ("private"); Cannot keep form information
Solution:

Add in front of Session_Start

Session_cache_limiter ("Private, must-revalidate");

Can.

2. Another way we can use Ajax to instantiate

The index.html template file is roughly as follows:

 

The contents of the login.php file are as follows:

<?php
Echo json_encode (Array (' username ' =>$_request[' username '), ' password ' =>$_request[' password ')) ;
? >

In this way, we do not need to refresh the page, if the failure will be submitted directly, so you can save the 100% commit failure after the data is not lost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.