PHP isset 形式 POST 值的简写
我正在创建一个表单,只是寻找更有效的方法来做事。到目前为止我所得到的是:
<p><input type="text" name="transmission" value="" /></p>
<p><input type="text" name="model" value="<?=$model;?>" /></p>
所以其中一些已经设置了值,而有些则为空。我想要做的是查看表单是否已设置,如果已设置,则使用 $_POST['name'] 作为值,如果没有,则使用空白或使用我之前的变量。
<p><input type="text" name="name" value="<?php if isset($_POST['submit'])) { echo $_POST['name']; } else { echo ""; } ?>" /></p>
但必须有一种更短的方法来做到这一点。
如果有人能指出我的方向,我将非常感激。
谢谢你!
I am creating a form and am just looking for more efficient ways to do things. What I have so far is:
<p><input type="text" name="transmission" value="" /></p>
<p><input type="text" name="model" value="<?=$model;?>" /></p>
So some of them will have a value already set, and some will be blank. What I want to do is see if the form has been set, and if it has then use $_POST['name'] as the value, if not then use either blank or use the previous variable I have.
<p><input type="text" name="name" value="<?php if isset($_POST['submit'])) { echo $_POST['name']; } else { echo ""; } ?>" /></p>
But there has to be a shorter way to do that.
IF anyone could point me in the direction I would really appreciate it.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
除了
(如果提交未设置,名称无论如何应该为空) - 您可能需要关闭警告。
话虽这么说,这是非常非常糟糕的做法,并且会让您面临跨站脚本攻击 (XSS)。您不应该这样做。即使在内联网上,如果攻击者拥有一台可以访问该计算机的计算机,他们就可以使用 XSS 执行用户可以执行的任何操作。
根据您的问题以及您对屏幕上这种类型的回声有多么厌恶,我建议您使用某种形式的模板库,例如 聪明。这使得你的 html 代码看起来像这样:
Is the shortest you'll get it, apart from say
<?= $_POST['name']; ?>
(if submit is not set name should be empty anyway) - you probably need to turn warnings off.All that being said, this is very very poor practice and opens you up to cross site scripting (XSS). You should NOT be doing this. Even on an intranet, if an attacker ever owns a computer that has access to it they can use XSS to perform any actions the user can.
From your question and how much distaste you have for this type of echoing on screen I'd suggest you use some form of templating library such as Smarty. This allows your html code to look like this:
我刚刚从 WordPress 编码标准中看到了这一点。尽管他们不鼓励可读性..
参考此处
I just saw this from wordpress coding standard. although they not encourage for the readability..
reference here
您可以在 HTML 输出之前在脚本开头定义变量,例如:
在 html 部分中,您可以打印 $name ,而不必担心它未定义。
此外,如果 $_POST['submit'] 不包含任何值,您更有可能收到 FALSE 声明。为了避免此类问题,请使用 array_key_exists
You can define variables in beginning of your script before HTML output, for example:
in your html section you can print $name without worrying it was not defined
Also if $_POST['submit'] does not contain any value you are more likely to receive FALSE statement. To avoid such issues use array_key_exists
正如 Nazariy 所说,您应该在模板中尽可能避免使用 PHP。
事实上,您应该在模板中只准备好变量。
所以,在你的代码中有这样的东西
,然后你就有了光滑整洁的模板:
Like Nazariy said, you should avoid as much PHP in the template as possible.
In fact, you should have in your template already prepared variables only.
So, in your code have something like this
and then you have smooth and neat template:
如果不深入探讨不使用
构建表单的原因,除了删除
else
之外,没有什么可以做的。Without going into the reasons not to use
<p>
to structure your forms, there's not much that can be done besides removing theelse
.简写
Shorthand
<?=$_POST['name'] ?: ''?>
使用php错误控制运算符:@。
如果设置了 POST 变量,它将被回显。如果没有,则不会显示任何内容。
Use the php error control operator: @.
If the POST variable is set, it will be echo-ed out. If not, nothing will show up.
你可以尝试这个...
我使用布尔比较而不是 isset 函数(请参阅表格链接)
http://www.php.net/manual/en/types.comparisons.php
或其他选项是...
You could try this...
I'm using a boolean compare instead of the isset function (see link for table)
http://www.php.net/manual/en/types.comparisons.php
or an other option is...