我的<input />框不会显示单引号

时间:2017-07-08 04:18:09

标签: php html

如果它中有单引号,我尝试输入HTML输入框的任何字符串在提交后都会在输入框中被截断。它的POST值没有变化,但字符串在输入框中显示为截断,无论我是否使用htmlspecialchars()。毫无疑问,这是一个noobie问题,但我已经努力想出来并且没有想法。谢谢你的帮助。

    <!DOCTYPE html>
    <body><title> Self-inserting input_box_SO.php </title>
    <?php
    //  POST form initiation.
    ECHO "<form action='input_box_SO.php' method='post'>"; 

    // GET POSTed value and escape it for HTML use
    $Caption_htmlspecialchars=$_POST['Caption_htmlspecialchars'];
    $Caption_htmlspecialchars=htmlspecialchars($Caption_htmlspecialchars);
    ECHO "The echo of the variable <em> \$Caption_htmlspecialchars </em> looks like this:<br>";
    ECHO "<b> $Caption_htmlspecialchars </b><br>";
    ECHO "But in the input box, \$Caption_htmlspecialchars is truncated by a single quote: <br>";
    // ETA: Bad old line that caused the problem, now commented:
    // ECHO "<input type='text' name='Caption_htmlspecialchars' size=100 value='$Caption_htmlspecialchars' maxlength = 100 required /><br><br>"; 
    // ETA: Newly added line that fixes the problem:
echo '<input type="text" name="Caption_htmlspecialchars" size=100 value="'.$Caption_htmlspecialchars.'" maxlength = 100 required /><br><br>';
    // SUBMIT button.  Submits back to the same page: input_box.php
    echo "<b><input type='Submit' name='submit' value='Submit'/></b></br></br>";
    ?>
    </body></html> 

enter image description here

以下是Inspect Elements&gt;元素显示输入元素:

input_box_SO.php 变量 $ Caption_htmlspecialchars 的回显如下所示:
使用特殊字符进行测试。 &安培; “&lt;&gt;和单引号('),导致输入框中的截断。
但在输入框中,$ Caption_htmlspecialchars被单引号截断:
和a单引号(“),=”“which =”“cause =”“truncation =”“in =”“the =”“input =”“box。=”“'=”“maxlength =”100“required =” “&gt;



Source看起来像这样:value='test with special chars. &amp; &quot; &lt; &gt; and a single quote ('), which causes truncation in the input box. '

2 个答案:

答案 0 :(得分:2)

您需要更改单引号序列和双引号以显示字符串。将.Expect()更改为

echo <input

答案 1 :(得分:0)

尝试使用addslashes并按照

进行操作

$Caption_htmlspecialchars = addslashes($Caption_htmlspecialchars);