Home  >  Article  >  Backend Development  >  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
WBOYOriginal
2016-07-20 11:12:17914browse

This article will introduce to you some summary 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.

PHP code
1.

tr>
The code is as follows Copy code
header('Cache-control: private, must-revalidate'); //Support page bounce
 代码如下 复制代码
header('Cache-control: private, must-revalidate'); //支持页面回跳

2. Use the session_cache_limiter method.

PHP code

The code is as follows Copy code
1.session_cache_limiter('private, must-revalidate'); //Write before the session_start method
 代码如下 复制代码
1.session_cache_limiter('private, must-revalidate'); //要写在session_start方法之前

The following is an introduction to 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: private mode cache (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 Reserved)

Set cache expiration time: session_cache_expire function setting, the default is 180 minutes.

Commonly encountered problems:

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, which cannot be automatically refreshed in time. If session_cache_limiter("private"); is not used, it cannot Retain form information

Solution:

Add

The code is as follows Copy code
 代码如下 复制代码

session_cache_limiter( "private, must-revalidate" );即可

session_cache_limiter( "private, must-revalidate" ); That's it


Another way we can use ajax to instance


index.html template file large content:

The code is as follows Copy code



jQuery Ajax Example Demonstration< /title><br></head><br><script src="./js/jquery.js" type="text/javascript"></script><br><script type= "text/javascript"><br>$(document).ready(function(){//This is jQueryready, it is like the main of C language and all operations are included in it<br> $("#button_login") .mousedown(function(){<br> login(); //The function login() is triggered after clicking the button with the ID "button_login";<br> });</p> <p>    function login(){ //函数 login();<br>        var username = $("#username").val();//取框中的用户名<br>        var password = $("#password").val();//取框中的密码<br>        $.ajax({ //一个Ajax过程<br>            type: "post", //以post方式与后台沟通<br>            url : "login.php", //与此php页面沟通<br>            dataType:'json',//从php返回的值以 JSON方式 解释<br>            data: 'username='+username+'&password='+password, //发给php的数据有两项,分别是上面传来的u和p<br>            success: function(json){//如果调用php成功<br>            //alert(json.username+'n'+json.password); //把php中的返回值(json.username)给 alert出来<br>            $('#result').html("姓名:" + json.username + "<br/>密码:" + json.password); //把php中的返回值显示在预定义的result定位符位置<br>            }<br>        });<br>    }<br>    //$.post()方式:<br>    $('#test_post').mousedown(function (){<br>        $.post(<br>            'login.php',<br>            {<br>            username:$('#username').val(),<br>            password:$('#password').val()<br>            },<br>            function (data) //回传函数<br>            {<br>                var myjson='';<br>                eval_r('myjson=' + data + ';');<br>                $('#result').html("姓名1:" + myjson.username + "<br/>密码1:" + myjson.password);<br>            }<br>        );<br>    });<br>    //$.get()方式:<br>    $('#test_get').mousedown(function (){<br>        $.get(<br>            'login.php',<br>            {<br>            username:$('#username').val(),<br>            password:$('#password').val()<br>            },<br>            function(data) //回传函数<br>            {<br>                var myjson='';<br>                eval_r("myjson=" + data + ";");<br>                $('#result').html("姓名2:" + myjson.username + "<br/>密码2:" + myjson.password);<br>            }<br>        );<br>    });<br>});<br></script><br><body><br><div id="result" style="background:orange;border:1px solid red;width:300px;height:200px;"></div><br><form id="formtest" action="" method="post"><br><p><span>Enter name:</span><input type="text" name="username" id="username" /></p><br><p><span>Enter password:</span><input type="text" name="password" id="password" /></p><br></form><br><button id="button_login">ajax submission</button><br><button id=" test_post">postsubmit</button><br><button id="test_get">getsubmit</button><br></body><br></html></p> </td> </tr> </table> <p> There seems to be a bug in the original source code, but it runs normally after modification. </p> <p>Contents of the login.php file: </p> <table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7 "> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> The code is as follows<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9071')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id="copy9071"><?php<br />echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));<br>?></td> </tr> </table> </td> <td width="109" align="center" bgcolor="#FFE7CE" style=" cursor:pointer;" onclick="doCopy('copy9071')">Copy code<p> </p> </td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id="copy9071"><?php</td>echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$ _REQUEST['password']));</td> </tr>?></table> <br> <p align="left">In this case, we don’t need to refresh the page when submitting. If it fails, there will be a submission directly, which is 100% The data will not be lost after the save and submit fails. </p> <div style="display:none;"> <span id="url" itemprop="url"> </span> <span id="indexUrl" itemprop="indexUrl"> </span> <span id="isOriginal" itemprop="isOriginal"> </span><span id="isBasedOnUrl" itemprop="isBasedOnUrl"></span>http://www.bkjia.com/PHPjc/444583.html<span id="genre" itemprop="genre"></span>www.bkjia.com<span id="description" itemprop="description"></span>true</div>http: //www.bkjia.com/PHPjc/444583.html<div class="art_confoot">TechArticle</div>This article will introduce to students how to retain the filled-in information after PHP fails to submit the form. Some methods are summarized, the most commonly used Just use caching. This method may be possible if the network speed is slow... </div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>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</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="WeChat Public Account Development Tutorial Part 5 - Receiving and Responding to Various Messages_PHP Tutorial" href="http://m.php.cn/faq/305951.html">WeChat Public Account Development Tutorial Part 5 - Receiving and Responding to Various Messages_PHP Tutorial</a></span><span>Next article:<a class="dBlack" title="WeChat Public Account Development Tutorial Part 5 - Receiving and Responding to Various Messages_PHP Tutorial" href="http://m.php.cn/faq/305953.html">WeChat Public Account Development Tutorial Part 5 - Receiving and Responding to Various Messages_PHP Tutorial</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="http://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ul class="nphpXgwzList"><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="http://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><div class="nphpFoot"><div class="nphpFootBg"><ul class="nphpFootMenu"><li><a href="http://m.php.cn/"><b class="icon1"></b><p>Home</p></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><p>Course</p></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><p>Q&A</p></a></li><li><a href="http://m.php.cn/login"><b class="icon5"></b><p>My</p></a></li><div class="clear"></div></ul></div></div><div class="nphpYouBox" style="display: none;"><div class="nphpYouBg"><div class="nphpYouTitle"><span onclick="$('.nphpYouBox').hide()"></span><a href="http://m.php.cn/"></a><div class="clear"></div></div><ul class="nphpYouList"><li><a href="http://m.php.cn/"><b class="icon1"></b><span>Home</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course.html"><b class="icon2"></b><span>Course</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/article.html"><b class="icon3"></b><span>Article</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/wenda.html"><b class="icon4"></b><span>Q&A</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/dic.html"><b class="icon6"></b><span>Dictionary</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/course/type/99.html"><b class="icon7"></b><span>Manual</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/xiazai/"><b class="icon8"></b><span>Download</span><div class="clear"></div></a></li><li><a href="http://m.php.cn/faq/zt" title="Topic"><b class="icon12"></b><span>Topic</span><div class="clear"></div></a></li><div class="clear"></div></ul></div></div><div class="nphpDing" style="display: none;"><div class="nphpDinglogo"><a href="http://m.php.cn/"></a></div><div class="nphpNavIn1"><div class="swiper-container nphpNavSwiper1"><div class="swiper-wrapper"><div class="swiper-slide"><a href="http://m.php.cn/" >Home</a></div><div class="swiper-slide"><a href="http://m.php.cn/article.html" class="hover">Article</a></div><div class="swiper-slide"><a href="http://m.php.cn/wenda.html" >Q&A</a></div><div class="swiper-slide"><a href="http://m.php.cn/course.html" >Course</a></div><div class="swiper-slide"><a href="http://m.php.cn/faq/zt" >Topic</a></div><div class="swiper-slide"><a href="http://m.php.cn/xiazai" >Download</a></div><div class="swiper-slide"><a href="http://m.php.cn/game" >Game</a></div><div class="swiper-slide"><a href="http://m.php.cn/dic.html" >Dictionary</a></div><div class="clear"></div></div></div><div class="langadivs" ><a href="javascript:;" class="bg4 bglanguage"></a><div class="langadiv" ><a onclick="javascript:setlang('zh-cn');" class="language course-right-orders chooselan " href="javascript:;"><span>简体中文</span><span>(ZH-CN)</span></a><a onclick="javascript:;" class="language course-right-orders chooselan chooselanguage" href="javascript:;"><span>English</span><span>(EN)</span></a><a onclick="javascript:setlang('zh-tw');" class="language course-right-orders chooselan " href="javascript:;"><span>繁体中文</span><span>(ZH-TW)</span></a><a onclick="javascript:setlang('ja');" class="language course-right-orders chooselan " href="javascript:;"><span>日本語</span><span>(JA)</span></a><a onclick="javascript:setlang('ko');" class="language course-right-orders chooselan " href="javascript:;"><span>한국어</span><span>(KO)</span></a><a onclick="javascript:setlang('ms');" class="language course-right-orders chooselan " href="javascript:;"><span>Melayu</span><span>(MS)</span></a><a onclick="javascript:setlang('fr');" class="language course-right-orders chooselan " href="javascript:;"><span>Français</span><span>(FR)</span></a><a onclick="javascript:setlang('de');" class="language course-right-orders chooselan " href="javascript:;"><span>Deutsch</span><span>(DE)</span></a></div></div><script> var swiper = new Swiper('.nphpNavSwiper1', { slidesPerView : 'auto', observer: true,//修改swiper自己或子元素时,自动初始化swiper observeParents: true,//修改swiper的父元素时,自动初始化swiper }); </script></div></div><!--顶部导航 end--><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body></html>