isset 表单 POST 值的 PHP 简写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7477966/
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
PHP Shorthand for isset form POST value
提问by Drew
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.
所以他们中的一些人已经设置了一个值,而一些人将是空白的。我想要做的是查看表单是否已设置,如果已设置,则使用 $_POST['name'] 作为值,如果没有,则使用空白或使用我拥有的前一个变量。
<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!
谢谢!
回答by Nazariy
You can define variables in beginning of your script before HTML output, for example:
您可以在 HTML 输出之前在脚本的开头定义变量,例如:
$name = isset($_POST['submit']) ? $_POST['name'] : null;
in your html section you can print $name without worrying it was not defined
在您的 html 部分,您可以打印 $name 而不用担心它没有被定义
<p><input type="text" name="name" value="<?php echo $name ?>" /></p>
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
此外,如果 $_POST['submit'] 不包含任何值,您更有可能收到 FALSE 语句。为避免此类问题,请使用array_key_exists
回答by Your Common Sense
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.
就像 Nazariy 所说的那样,您应该尽可能避免在模板中使用 PHP。
事实上,您的模板中应该只包含已经准备好的变量。
So, in your code have something like this
所以,在你的代码中有这样的东西
$FORM = array();
$form_fields = array('name','sex');
foreach($form_fields as $fname) {
if (isset($_POST[$fname])) {
$FORM[$fname] = htmlspecialchars($_POST[$fname]);
} else {
$FORM[$fname] ='';
}
}
and then you have smooth and neat template:
然后你有光滑整洁的模板:
<p><input type="text" name="name" value="<?=$FORM['name']?>" /></p>
回答by Christian
Shorthand <?=$_POST['name'] ?: ''?>
速记 <?=$_POST['name'] ?: ''?>
回答by adlawson
Without going into the reasons not to use <p>
to structure your forms, there's not much that can be done besides removing the else
.
在不考虑不使用<p>
来构建表单的原因的情况下,除了删除else
.
<p><input type="text" name="name" value="<?php if isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
回答by Csibe
Use the php error controloperator: @.
使用php 错误控制运算符:@。
<?php
$posted_text = @$_POST['posted_text'];
echo $posted_text
?>
If the POST variable is set, it will be echo-ed out. If not, nothing will show up.
如果设置了 POST 变量,它将被回显。如果没有,则不会显示任何内容。
回答by Terence
You could try this...
你可以试试这个...
$_POST['submit'] ? echo $_POST['name'] : echo '';
I'm using a boolean compare instead of the isset function (see link for table)
我正在使用布尔比较而不是 isset 函数(请参阅表格链接)
http://www.php.net/manual/en/types.comparisons.php
http://www.php.net/manual/en/types.comparisons.php
or an other option is...
或者另一种选择是......
echo ($_POST['submit'] ? $_POST['name'] : '');
回答by Louis
<?= isset($_POST['submit'])? $_POST['name'] : ""; ?>
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.
是最短的你会得到它,除了说<?= $_POST['name']; ?>
(如果提交没有设置名称应该是空的) - 你可能需要关闭警告。
All that being said, this is very very poor practiceand opens you up to cross site scripting (XSS). You should NOTbe 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.
话虽如此,这是非常糟糕的做法,并且会让您接触跨站点脚本 (XSS)。你不应该这样做。即使在 Intranet 上,如果攻击者拥有可以访问它的计算机,他们也可以使用 XSS 执行用户可以执行的任何操作。
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:
从您的问题以及您对屏幕上这种类型的回声有多少反感,我建议您使用某种形式的模板库,例如Smarty。这允许您的 html 代码如下所示:
<p><input type="text" name="name" value="{name}" /></p>