Home > Backend Development > PHP Tutorial > How to retain the filled-in information after PHP fails to submit the form_PHP Tutorial

How to retain the filled-in information after PHP fails to submit the form_PHP Tutorial

WBOY
Release: 2016-07-13 10:24:33
Original
783 people have browsed it

This article introduces a summary of some 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.

The PHP code is as follows:

header('Cache-control: private, must-revalidate'); //支持页面回跳 

Copy after login

2. Use the session_cache_limiter method.

The PHP code is as follows:

session_cache_limiter('private, must-revalidate'); //要写在session_start方法之前 

Copy after login

The session_cache_limiter parameters are introduced below:

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: cache in private mode (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)
Set cache expiration time: session_cache_expire function setting, the default is 180 minutes.

Commonly encountered questions:

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 and cannot be automatically refreshed in time. If session_cache_limiter("private" is not used) "); and the form information cannot be retained
Solution:

Add

in front of session_start
session_cache_limiter( "private, must-revalidate" );

Copy after login

That’s it.

2. Another way we can use ajax to instantiate

The approximate content of the index.html template file is as follows:

<html>
<head>
<title>jQuery Ajax 实例演示</title>
</head>
<script src="./js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){//这个就是jQueryready ,它就像C语言的main 所有操作包含在它里面
 $("#button_login").mousedown(function(){
 login(); //点击ID为"button_login"的按钮后触发函数 login();
 });
 function login(){ //函数 login();
  var username = $("#username").val();//取框中的用户名
  var password = $("#password").val();//取框中的密码
  $.ajax({ //一个Ajax过程
   type: "post", //以post方式与后台沟通
   url : "login.php", //与此php页面沟通
   dataType:'json',//从php返回的值以 JSON方式 解释
   data: 'username='+username+'&password='+password, //发给php的数据有两项,分别是上面传来的u和p
   success: function(json){//如果调用php成功
   //alert(json.username+'n'+json.password); //把php中的返回值(json.username)给 alert出来
   $('#result').html("姓名:" + json.username + "<br/>密码:" + json.password); //把php中的返回值显示在预定义的result定位符位置
   }
  });
 }
 //$.post()方式:
 $('#test_post').mousedown(function (){
  $.post(
   'login.php',
   {
   username:$('#username').val(),
   password:$('#password').val()
   },
   function (data) //回传函数
   {
    var myjson='';
    eval_r('myjson=' + data + ';');
    $('#result').html("姓名1:" + myjson.username + "<br/>密码1:" + myjson.password);
   }
  );
 });
 //$.get()方式:
 $('#test_get').mousedown(function (){
  $.get(
   'login.php',
   {
   username:$('#username').val(),
   password:$('#password').val()
   },
   function(data) //回传函数
   {
    var myjson='';
    eval_r("myjson=" + data + ";");
    $('#result').html("姓名2:" + myjson.username + "<br/>密码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>输入姓名:</span><input type="text" name="username" id="username" /></p>
<p><span>输入密码:</span><input type="text" name="password" id="password" /></p>
</form>
<button id="button_login">ajax提交</button>
<button id="test_post">post提交</button>
<button id="test_get">get提交</button>
</body>
</html>

Copy after login

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

<&#63;php
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));
&#63;>

Copy after login

In this case, we don’t need to refresh the page to submit. If it fails, it will be submitted directly. This way, the data will not be lost after the submission fails.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825419.htmlTechArticleThis article introduces a summary of some methods on how to retain the filled-in information after PHP fails to submit the form. The most common one is to use caching. Yes, this method may cause problems if the network speed is slow,...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template