Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.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_Forms_Input_Echo_Backslash - Fatal编程技术网

Php 用反斜杠回显表单输入

Php 用反斜杠回显表单输入,php,forms,input,echo,backslash,Php,Forms,Input,Echo,Backslash,尝试使用echo使html代码正常工作 它以html的形式工作: <input type="text" name="ordernum" value="<?= isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '' ?>" /> 作为: 不能只在字符串文字中间编写任意PHP代码 这是因为$\u POST['ordernum']部分 重新这样写: $val = isset($_POST["o

尝试使用echo使html代码正常工作

它以html的形式工作:

<input type="text" name="ordernum" value="<?= isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '' ?>" />
作为:


不能只在字符串文字

中间编写任意PHP代码 这是因为
$\u POST['ordernum']
部分

重新这样写:

$val = isset($_POST["ordernumW]) ? htmlspecialchars($_POST["ordernum"]) : "";

echo "<input type='text' name='ordernum' value='$val' />";
$val=isset($\u POST[“ordernumW])?htmlspecialchars($\u POST[“ordernum”]):“”;
回声“;
不能在字符串文字中使用任意代码。首先在一个变量中赋值,然后在
echo
语句中使用该变量

$orderNum = isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '';
echo ' <input type="text" name="ordernum" value="'.$orderNum.'" />';
$orderNum=isset($\u POST['orderNum'])?htmlspecialchars($\u POST['orderNum']):“”;
回声';

您需要使用
\
字符串结尾的字符进行转义(特别是字符串分隔符

这两条线起作用:

echo "<input type=\"text\" name=\"ordernum\" value=\"" . (isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '') . "\" />";
echo '<input type="text" name="ordernum" value="' . (isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '') . '" />';
echo”“;
回声';

这就是它应该是什么:

echo('<input type="text" name="ordernum" value="'.(isset($_POST['ordernum'])?htmlspecialchars($_POST['ordernum']):'').'">');
像这样:

"first part of string".$myvariable."last part of string";
您只需转义包含字符串的引号类型,还需要:

"I need to escape this \" but not this ' "
'I need to escape this \' but not this " '
$val=isset($\u POST['ordernum'])?htmlspecialchars($_POST['ordernum']):“”;
回声';

< /代码>不能只在字符串文字的中间编写任意PHP代码。三值条件在字符串定义中是完全有效的。您只需删除
标记并正确连接;-)@user3556674确定并接受此答案并结束此问题。这比三元if语句有什么好处?正如前面所说的,您不能在字符串中使用任意条件。此代码完全有效。三元条件的计算结果为
$\u POST['ordernum']
的HTML转义版本或连接到第一部分的空字符串。您不能在字符串定义中使用
if
语句,但没有任何东西可以阻止您连接文字、函数结果或三元条件。
"first part of string".$myvariable."last part of string";
"I need to escape this \" but not this ' "
'I need to escape this \' but not this " '
$val = isset($_POST['ordernum']) ? htmlspecialchars($_POST['ordernum']) : '';

echo '<input type="text" name="ordernum" value="'. $val. '" />';