PHP isset 形式 POST 值的简写

发布于 2024-12-05 10:04:03 字数 573 浏览 0 评论 0原文

我正在创建一个表单,只是寻找更有效的方法来做事。到目前为止我所得到的是:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

左秋 2024-12-12 10:04:04
<?= isset($_POST['submit'])? $_POST['name'] : ""; ?>

除了 (如果提交未设置,名称无论如何应该为空) - 您可能需要关闭警告。

话虽这么说,这是非常非常糟糕的做法,并且会让您面临跨站脚本攻击 (XSS)。您不应该这样做。即使在内联网上,如果攻击者拥有一台可以访问该计算机的计算机,他们就可以使用 XSS 执行用户可以执行的任何操作。

根据您的问题以及您对屏幕上这种类型的回声有多么厌恶,我建议您使用某种形式的模板库,例如 聪明。这使得你的 html 代码看起来像这样:

<p><input type="text" name="name" value="{name}" /></p>
<?= 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.

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:

<p><input type="text" name="name" value="{name}" /></p>
裂开嘴轻声笑有多痛 2024-12-12 10:04:04

我刚刚从 WordPress 编码标准中看到了这一点。尽管他们不鼓励可读性..

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

参考此处

I just saw this from wordpress coding standard. although they not encourage for the readability..

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

reference here

月光色 2024-12-12 10:04:03

您可以在 HTML 输出之前在脚本开头定义变量,例如:

$name = isset($_POST['submit']) ? $_POST['name'] : null;

在 html 部分中,您可以打印 $name ,而不必担心它未定义。

<p><input type="text" name="name" value="<?php echo $name ?>" /></p>

此外,如果 $_POST['submit'] 不包含任何值,您更有可能收到 FALSE 声明。为了避免此类问题,请使用 array_key_exists

You can define variables in beginning of your script before HTML output, for example:

$name = isset($_POST['submit']) ? $_POST['name'] : null;

in your html section you can print $name without worrying it was not defined

<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

九公里浅绿 2024-12-12 10:04:03

正如 Nazariy 所说,您应该在模板中尽可能避免使用 PHP。
事实上,您应该在模板中只准备好变量。

所以,在你的代码中有这样的东西

$FORM = array();
$form_fields = array('name','sex');
foreach($form_fields as $fname) {
  if (isset($_POST[$fname])) {
    $FORM[$fname] = htmlspecialchars($_POST[$fname]);
  } else {
    $FORM[$fname] ='';
  }
}

,然后你就有了光滑整洁的模板:

<p><input type="text" name="name" value="<?=$FORM['name']?>" /></p>

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

$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>
吾性傲以野 2024-12-12 10:04:03

如果不深入探讨不使用​​

构建表单的原因,除了删除 else 之外,没有什么可以做的。

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

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><input type="text" name="name" value="<?php if isset($_POST['name'])) echo $_POST['name']; ?>" /></p>
感受沵的脚步 2024-12-12 10:04:03

简写

Shorthand <?=$_POST['name'] ?: ''?>

晚风撩人 2024-12-12 10:04:03

使用php错误控制运算符:@。

<?php

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

?>  

如果设置了 POST 变量,它将被回显。如果没有,则不会显示任何内容。

Use the php error control operator: @.

<?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.

涙—继续流 2024-12-12 10:04:03

你可以尝试这个...

$_POST['submit'] ? echo $_POST['name'] : echo '';

我使用布尔比较而不是 isset 函数(请参阅表格链接)

http://www.php.net/manual/en/types.comparisons.php

或其他选项是...

echo ($_POST['submit'] ? $_POST['name'] : '');

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)

http://www.php.net/manual/en/types.comparisons.php

or an other option is...

echo ($_POST['submit'] ? $_POST['name'] : '');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文