php 提交后如何在文本输入中保留值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2296347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I keep a value in a text input after a submit occurs?
提问by initall
I want to validate a form to make sure a user writes in a name and last name.
我想验证表单以确保用户填写姓名和姓氏。
If a user writes in only his last name, the form should show again with an error message beside the last name field, but the name value should stillbe there.
如果用户只在他的姓写,形式应与姓氏字段旁边的错误信息再次显示,但名称值应该仍然在那里。
I don't know how to do this, hehe. I'm just a newbie PHP programmer.
我不知道该怎么做,嘿嘿。我只是一个新手 PHP 程序员。
Here's what I have so far:
这是我到目前为止所拥有的:
<html>
<head>
</head>
<body>
<form action="formprueba.php" method="get">
<dl>
<dt>Last Name:</dt>
<dd><input type="text" value="" name="name" />
<?php if((isset($_GET['name'])) && ($_GET['name'] == "")){
echo "Please write a correct name.";
}
?>
</dd>
<dt>Last Name:</dt>
<dd><input type="text" value="" name="lastname" />
<?php if((isset($_GET['lastname'])) && ($_GET['lastname'] == "")){
echo "Please write a correct last name.";
}
?>
</dd>
<br />
<dt>
<input type="submit" value="enviar" />
</dt>
<input type="hidden" name="enviado" value="j"/>
</dl>
</form>
</body>
Also, I'm sure this validation is horrible, if you have a couple of minutes, I'd be really grad if someone can show me a more efficient way to validate (without using third party addons).
另外,我确定这种验证很糟糕,如果您有几分钟的时间,如果有人可以向我展示一种更有效的验证方式(不使用第三方插件),我真的很高兴。
Thanks SO.
谢谢。
回答by initall
Place the variable you need into the "value" field of your input-tag like this:
将您需要的变量放入输入标签的“值”字段中,如下所示:
<input type="text" value="<?php echo htmlspecialchars($_GET['lastname']); ?>" name="lastname" />
AND it's important to quote at a minimum the HTML characters someone might put in (by htmlspecialchars).
并且至少引用某人可能输入的 HTML 字符(通过 htmlspecialchars)很重要。
回答by Sarfraz
If a user writes in only his last name, the form should show again with an error message beside the last name field, but the name value should still be there.
如果用户只输入他的姓氏,则表单应再次显示并在姓氏字段旁边显示错误消息,但名称值仍应存在。
Just get their value from $_GET(the form method you are using)
只需从$_GET(您正在使用的表单方法)中获取它们的值
<input type="text" value="" name="lastname" value="<?php echo $_GET['lastname'];?>" />
and so on....
等等....
You mustalso use htmlspecialcharsfunction for security reasons.
出于安全原因,您还必须使用htmlspecialchars函数。
回答by chros
If you are doing something like
如果你正在做类似的事情
<input type="text" value="" name="lastname" value="<?php echo $_GET['lastname'];?>" />
never never ever forget to check/escape the value you display using e.g. htmlentities(). Otherwise you are open to completely trivial javascript injection attacks!
永远不要忘记检查/转义您使用例如 htmlentities() 显示的值。否则你很容易受到完全无关紧要的 javascript 注入攻击!
回答by chros
Its good to do validations on the server side. Your approach seems to be right. You can just maintain a flag which will tell you at the end of the page weather or not the validation was successful.
在服务器端进行验证很好。你的方法似乎是正确的。您可以只维护一个标志,它会在页面结束时告诉您天气或验证是否成功。
Example
例子
$flag=ture;
$flag=ture;
<?php if((isset($_GET['lastname'])) && ($_GET['lastname'] == "")){
echo "Please write a correct last name.";
$flag="false";
}
?>
then at the bottom of the page if($flag==true) echo "form submit";
然后在页面底部 if($flag==true) echo "form submit";
About persistence of value one of the person has correctly mention adding value to your input tag
关于价值的持久性,有人正确地提到为您的输入标签增加价值
value="<?php echo $_GET['lastname'];?>"
Hope this helps.
希望这可以帮助。
回答by jldupont
You could use client side javascript - you save a trip to the server this way.
您可以使用客户端 javascript - 这样可以节省到服务器的行程。
The downside though : you can really only do basic checking as the user cannot be trusted (for security measures of course ). You can at least "flag" the invalid patterns before making an expensive trip to the server.
但缺点是:您实际上只能进行基本检查,因为用户不可信(当然是为了安全措施)。在对服务器进行昂贵的访问之前,您至少可以“标记”无效模式。
回答by chros
just a small note i had to learn the hard way: displaying an error message and urging the user pressing the back-button (or doing the same using javascript) does notin general preserve values in the input fields.
只是一个小笔记,我必须以艰难的方式学习:显示错误消息并敦促用户按下后退按钮(或使用 javascript 执行相同操作)通常不会保留输入字段中的值。
This seems to be completely browser-specific, e.g. for me it worked on Safari, but not on Firefox...
这似乎完全是特定于浏览器的,例如对我来说它在 Safari 上工作,但在 Firefox 上不起作用......
回答by Learner
Add this to your form where you first declare it:
将此添加到您首先声明的表单中:
<form onsubmit="event.preventDefault(); validateMyForm();">
回答by Kangkan
I think, Javascript is the best answer if you can rely on Javascript. But client side script can be over-ridden. So validate your input on the server later.
我认为,如果您可以依靠 Javascript,那么 Javascript 是最好的答案。但是客户端脚本可以被覆盖。因此,稍后在服务器上验证您的输入。

