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
WBOYOriginal
2016-07-20 11:12:17977browse

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="https://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="https://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="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://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="https://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="https://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="https://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="https://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><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><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>