Home Backend Development PHP Tutorial PHP提交表单失败后如何保留已经填写的信息_PHP

PHP提交表单失败后如何保留已经填写的信息_PHP

Jun 01, 2016 am 11:51 AM

本文介绍PHP提交表单失败后如何保留填写的信息一些方法总结,其中最常用的就是使用缓存方式了,这种方法如果网速慢是可能出问题的,最好的办法就是使用ajax了。

1.使用header头设置缓存控制头Cache-control。

PHP代码如下:

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

Copy after login

2.使用session_cache_limiter方法。

PHP代码如下:

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

Copy after login

下面介绍一下session_cache_limiter参数:

session_cache_limiter内的几个参数意义是:
nocache:当然是不缓存(比如:表单信息被清除),但公共变量可以缓存
private:私有方式缓存(比如:表单信息被保留,但在生存期内有效)
private_no_cache:私有方式但不过期(表单信息被保留)
publice:公有方式,(表单信息也被保留)
设置缓存过期时间:session_cache_expire函数设置,缺省是180分钟。

常遇见问题:

1.session_cache_limiter("private");表单信息是保留了,但是如果我修改已经提交的信息,表单页面所呈现的信息还是缓存里信息,没能及时自动刷新,如果不用session_cache_limiter("private");又不能保留表单信息
解决方案:

在session_start前面加上

session_cache_limiter( "private, must-revalidate" );

Copy after login

即可。

2.另一种办法我们可以使用ajax来实例

index.html模板文件大致内容如下:

<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

login.php文件的内容如下:

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

Copy after login

这样的话我们提交不需要刷新页面了,如果失败就直接会有提交了,这样可以100%保存提交失败后数据不被丢失了。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles