PHP提交表單失敗後保留已填寫資訊怎麼實現?

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   get   

PHP提交表單失敗後如何保留已經填寫的資訊。

PHP提交表單失敗後如何保留填寫的資訊一些方法總結,其中最常用的就是使用緩衝方式了,這種方法如果網速慢是可能出問題的,最好的辦法就是使用ajax了。
1.使用header頭設定緩衝控制頭Cache-control。
PHP代碼:
header(‘Cache-control: private, must-revalidate‘); //支援頁面回跳

2.使用session_cache_limiter方法。
PHP代碼:
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");表單資訊是保留了,但是如果(www.jbxue.com)修改已提交資訊,表單頁面所呈現的資訊還是緩衝裡資訊,沒能及時自動重新整理,如果不用session_cache_limiter("private");又不能保留表單資訊
解決方案:
在session_start前面加上
session_cache_limiter( "private, must-revalidate" );
即可。
2.另一種辦法我們可以使用ajax來執行個體
index.html模板檔案大致內容如下:

<html><head><title>jQuery Ajax 執行個體示範--www.jbxue.com</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和psuccess: 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>

login.php檔案的內容:

<?phpecho json_encode(array (‘username‘=>$_REQUEST[‘username‘],‘password‘=>$_REQUEST[‘password‘]));?>

這樣,提交不需要重新整理頁面了,如果失敗就直接會有提交了,這樣可以100%儲存提交失敗後資料不被丟失了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.