Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用三元运算符更改输入属性_Php_Html_Input_Attributes_Ternary Operator - Fatal编程技术网

Php 使用三元运算符更改输入属性

Php 使用三元运算符更改输入属性,php,html,input,attributes,ternary-operator,Php,Html,Input,Attributes,Ternary Operator,这个想法是,我在一个表单中有一堆输入,当我发送它(到同一个页面)时,我希望保留所选的选项值 这就是我如何使它工作的原因 <input type="number" <?= isset($_POST['price']) ? 'value="' . $_POST['price'] . '"' : '' ?> > 您可以使用空合并运算符并在value属性中输出输入的数据: <input type="number" value="<?= $_POST['

这个想法是,我在一个表单中有一堆输入,当我发送它(到同一个页面)时,我希望保留所选的选项值

这就是我如何使它工作的原因

<input type="number"  
    <?= isset($_POST['price']) ? 'value="' . $_POST['price'] . '"' : '' ?> 
>

您可以使用空合并运算符并在value属性中输出输入的数据:

<input type="number" value="<?= $_POST['price'] ?? '' ?>">

Danger:此代码是在插入HTML文档之前需要转义的用户输入!。危险:此代码是在插入HTML文档之前需要转义的用户输入!。谢谢,那好多了。我还没有真正研究过这种漏洞,你能推荐一些资源吗?
<input type="number" value="<?= htmlentities($_POST['price'] ?? '') ?>">
<input type="number" value=
"<?= isset($_POST['price']) ? htmlentities($_POST['price']) : '' ?>"
>