How to keep the completed information when PHP submits the form failed

Source: Internet
Author: User
Tags json php file php code

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

PHP code
1.

The code is as follows Copy Code
Header (' Cache-control:private, Must-revalidate '); Support page Bounce

2. Use the Session_cache_limiter method.

PHP code

The code is as follows Copy Code
1.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 that has been 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"), and cannot keep form information

Solution:

Add in front of Session_Start

The code is as follows Copy Code

Session_cache_limiter ("Private, must-revalidate");


Another way we can use Ajax to instantiate


index.html template file Large content:

The code is as follows Copy Code

<title>jquery Ajax Example Demo </title>
<script src= "./js/jquery.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {//This is Jqueryready, it's like the C language main all operations contained inside it
$ ("#button_login"). MouseDown (function () {
Login (); Click the button with ID "Button_login" to trigger the function login ();
});

function login () {//functions login ();
var username = $ ("#username"). Val ();//The user name in the Fetch box
var password = $ ("#password"). Val ();//password in the Fetch box
$.ajax ({//an AJAX process
Type: "POST",//post to communicate with the background
URL: "login.php",//Communication with this PHP page
DataType: ' json ',//return value from PHP interpreted in JSON
Data: ' username= ' +username+ ' &password= ' +password,//php to be sent to two items, the above is the U and P
Success:function (JSON) {//If PHP is invoked successfully
Alert (json.username+ ' n ' +json.password); Send the return value (json.username) in PHP to alert
$ (' #result '). HTML ("Name:" + json.username + "<br/> Password:" + Json.password); Display the return value in PHP at the predefined result locator location
}
});
}
$.post () Way:
$ (' #test_post '). MouseDown (function () {
$.post (
' Login.php ',
{
username:$ (' #username '). Val (),
password:$ (' #password '). Val ()
},
function (data)//postback functions
{
var myjson= ';
Eval_r (' myjson= ' + data + '; ');
$ (' #result '). HTML ("Name 1:" + myjson.username + "<br/> password 1:" + Myjson.password);
}
);
});
$.get () Way:
$ (' #test_get '). MouseDown (function () {
$.get (
' Login.php ',
{
username:$ (' #username '). Val (),
password:$ (' #password '). Val ()
},
function (data)//postback functions
{
var myjson= ';
Eval_r ("myjson=" + Data + ";");
$ (' #result '). HTML ("Name 2:" + myjson.username + "<br/> Password 2:" + Myjson.password);
}
);
});
});
</script>
<body>
<div id= "Result" style= "background:orange;border:1px solid red;width:300px;height:200px;" ></div>
<form id= "formtest" action= "" method= "POST" >
<p><span> Enter Name: </span><input type= "text" name= "username" id= "username"/></p>
<p><span> input Password: </span><input type= "text" name= "password" id= "password"/></p>
</form>
<button id= "Button_login" >ajax submit </button>
<button id= "Test_post" >post submit </button>
<button id= "Test_get" >get submit </button>
</body>

The original source code seems to have a little bug, modified normal operation.

Contents of login.php File:

The code is as follows Copy Code
<?php
echo json_encode (Array (' username ' =>$_request[' username '), ' password ' =>$_request[' password '));
?>

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

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.