简体   繁体   English

PHP isset形式POST值的简写

[英]PHP Shorthand for isset form POST value

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! 谢谢!

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. 此外,如果$ _POST ['submit']不包含任何值,您更有可能收到FALSE语句。 To avoid such issues use array_key_exists 要避免此类问题,请使用array_key_exists

Like Nazariy said, you should avoid as much PHP in the template as possible. 就像Nazariy说的那样,你应该尽可能避免模板中的PHP。
In fact, you should have in your template already prepared variables only. 实际上,您应该只在模板中准备好变量。

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>

速记<?=$_POST['name'] ?: ''?>

在没有使用<p>构建表单的原因的情况下,除了删除else之外,没有太多可以做的else

<p><input type="text" name="name" value="<?php if isset($_POST['name'])) echo $_POST['name']; ?>" /></p>

I just saw this from wordpress coding standard. 我刚刚从wordpress编码标准中看到了这个。 although they not encourage for the readability.. 虽然他们不鼓励可读性..

isset( $var ) || $var = some_function();

reference here 这里参考

Use the php error control operator: @. 使用php错误控制操作符:@。

<?php

   $posted_text = @$_POST['posted_text'];
   echo $posted_text

?>  

If the POST variable is set, it will be echo-ed out. 如果设置了POST变量,它将被回显出来。 If not, nothing will show up. 如果没有,任何事都不会出现。

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'] : '');
<?= isset($_POST['submit'])? $_POST['name'] : ""; ?>

Is the shortest you'll get it, apart from say <?= $_POST['name']; ?> 除了说<?= $_POST['name']; ?>之外,你得到的最短<?= $_POST['name']; ?> <?= $_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 practice and opens you up to cross site scripting (XSS) . 所有这一切,这是非常糟糕的做法,并打开你跨站点脚本(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. 即使在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 . 根据你的问题以及你对屏幕上这种类型的回声有多少厌恶,我建议你使用某种形式的模板库,比如Smarty This allows your html code to look like this: 这允许你的HTML代码看起来像这样:

<p><input type="text" name="name" value="{name}" /></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM