提交PHP后将值保存在表单中
我从一个控制器调用这些函数来获取表单和表单中的值。我的问题是,在提交失败后,如何在表单中保留值?我试过这样的方法:提交PHP后将值保存在表单中,php,forms,Php,Forms,我从一个控制器调用这些函数来获取表单和表单中的值。我的问题是,在提交失败后,如何在表单中保留值?我试过这样的方法: <input type="text" name="myField1" value="<?php echo isset($_POST['myField1']) ? $_POST['myField1'] : '' ?>" /> [编辑:在本地主机上运行此命令] autocomplete不适用于所有浏览器;而且,这只是一个提示,所以cookies是剩下的唯一
<input type="text" name="myField1" value="<?php echo isset($_POST['myField1']) ? $_POST['myField1'] : '' ?>" />
[编辑:在本地主机上运行此命令]
autocomplete
不适用于所有浏览器;而且,这只是一个提示,所以cookies是剩下的唯一选项。下面的解决方案运行良好,但是如果其他用户为这个流行的请求提供其他方法,那就太好了:在提交后保持表单中的值
<html>
<head>
<script>
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function store() {
var inputs, index;
inputs = document.getElementsByTagName('input');
for (index = 0; index < inputs.length - 1; ++index) {
setCookie(inputs[index].name,inputs[index].value,1);
}
return false;
}
</script>
</head>
<body>
<form method="post" action="back.php" onsubmit="store()" >
firstname<input type="text" name="firstname">
lastname<input type="text" name="lastname">
emailid<input type="text" name="emailid">
<input type="submit" >
</form>
<script>
(function load(){
var inputs, index;
inputs = document.getElementsByTagName('input');
for (index = 0; index < inputs.length - 1; ++index) {
inputs[index].value = getCookie(inputs[index].name);
}
return false;
})();
</script>
</body>
</html>
函数setCookie(c_名称、值、exdays){
var exdate=新日期();
exdate.setDate(exdate.getDate()+exdays);
var c_value=escape(value)+(exdays==null)?“”:“expires=“+exdate.toutString());
document.cookie=c_name+“=”+c_值;
}
函数getCookie(c_名称){
变量i,x,y,ARRcookies=document.cookie.split(“;”);
对于(i=0;i<0.length;i++){
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf(“=”);
y=ARRcookies[i].substr(ARRcookies[i].indexOf(“=”)+1);
x=x.replace(/^\s+|\s+$/g,”);
如果(x==c_名称){
返回unescape(y);
}
}
}
函数存储(){
var输入,指数;
输入=document.getElementsByTagName('input');
对于(索引=0;索引
[编辑:在本地主机上运行此命令]
autocomplete
不适用于所有浏览器;而且,这只是一个提示,所以cookies是剩下的唯一选项。下面的解决方案运行良好,但是如果其他用户为这个流行的请求提供其他方法,那就太好了:在提交后保持表单中的值
<html>
<head>
<script>
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function store() {
var inputs, index;
inputs = document.getElementsByTagName('input');
for (index = 0; index < inputs.length - 1; ++index) {
setCookie(inputs[index].name,inputs[index].value,1);
}
return false;
}
</script>
</head>
<body>
<form method="post" action="back.php" onsubmit="store()" >
firstname<input type="text" name="firstname">
lastname<input type="text" name="lastname">
emailid<input type="text" name="emailid">
<input type="submit" >
</form>
<script>
(function load(){
var inputs, index;
inputs = document.getElementsByTagName('input');
for (index = 0; index < inputs.length - 1; ++index) {
inputs[index].value = getCookie(inputs[index].name);
}
return false;
})();
</script>
</body>
</html>
函数setCookie(c_名称、值、exdays){
var exdate=新日期();
exdate.setDate(exdate.getDate()+exdays);
var c_value=escape(value)+(exdays==null)?“”:“expires=“+exdate.toutString());
document.cookie=c_name+“=”+c_值;
}
函数getCookie(c_名称){
变量i,x,y,ARRcookies=document.cookie.split(“;”);
对于(i=0;i<0.length;i++){
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf(“=”);
y=ARRcookies[i].substr(ARRcookies[i].indexOf(“=”)+1);
x=x.replace(/^\s+|\s+$/g,”);
如果(x==c_名称){
返回unescape(y);
}
}
}
函数存储(){
var输入,指数;
输入=document.getElementsByTagName('input');
对于(索引=0;索引
在表单之前,实例化将接收$\u POST
数据的对象
$userbox = new Userbox;
然后处理$\u POST
数据,如果有:
if(isset($_POST['submit']) && $_POST['submit'] === 'userboxsubmit'){
$userbox->process_post();
}
然后输出表单:
<input type="text" name="myField1" value="<?php echo $userbox->myField1; ?>" />
在表单之前,实例化将接收$\u POST
数据的对象
$userbox = new Userbox;
然后处理$\u POST
数据,如果有:
if(isset($_POST['submit']) && $_POST['submit'] === 'userboxsubmit'){
$userbox->process_post();
}
然后输出表单:
<input type="text" name="myField1" value="<?php echo $userbox->myField1; ?>" />
在我看来很好,这是代码的顶层。它永远不会填满?最好的答案是:当我使用代码的顶层时,只会不断出现语法错误,无法找出错误所在。语法错误的文本是什么?我对你的答案投了赞成票,因为这是一个重要的概念,在我看来很好,代码的顶层。它永远不会填充?最好的答案是:当我使用代码的顶部位时,只会不断出现语法错误,无法找出错误。语法错误的文本是什么?我对你的答案进行了优化,因为这是一个重要的概念使用该方法:D无需额外费用,简单的纯html:D但这不会保留字段中的值,只是预测值,对吧?它可以保持,但我在所有浏览器上都会检查它,而且我也在用javascript的方式使用这种方法:D不需要额外收费,简单的html:D但这不会在字段中保留值,只是预测值,对吧?它可以保持,但我在所有浏览器上都会检查它,同时也在努力用javascript实现这一点