search
Homephp教程php手册PHP提交表单失败后如何保留填写的信息

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

Jun 13, 2016 am 11:28 AM
phpintroducereserveinformationclassmatebackfailhowsubmitarticleform

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

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

PHP代码
1.

 代码如下 复制代码
header('Cache-control: private, must-revalidate'); //支持页面回跳

2.使用session_cache_limiter方法。

PHP代码

 代码如下 复制代码
1.session_cache_limiter('private, must-revalidate'); //要写在session_start方法之前

下面介绍一下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" );即可


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


index.html模板文件大内容:

 代码如下 复制代码



jQuery Ajax 实例演示






输入姓名:


输入密码:







最初的源码里面好像有一点bug,修改后正常运行。

login.php文件的内容:

 代码如下 复制代码
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));
?>

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


Statement
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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)