使用字符串引用时,只有第一个字符串出现在HTML表单中

时间:2013-12-02 18:59:28

标签: php html string forms

我遇到了HTML表单和php的问题。

我正在尝试将表单文本字段的默认值设置为预定义字符串。为此,我正在使用参考。但是,每当我尝试使用PHP或任何类型的形式或形式的引用将字符串放入表单时,只会添加第一个单词。

以下是描述情况的图片:

enter image description here

有谁知道为什么会这样,和/或解决这个问题的方法?

实际代码:

<?php 
    Echo "<input type=\"text\" name=\"Address\" value=\"one two three\"><br>";
?>

<?php 
    $str ="one two three";
    Echo "<input type=\"text\" name=\"Address\" value=" . $str . "><br>";
?>

<input type="text" name="Address" value=<?php echo "one two three";?>><br>
<input type="text" name="Address" value=<?php $str = "one two three"; echo $str;?>><br>
<input type="text" name="Address" value="one two three"><br>

4 个答案:

答案 0 :(得分:7)

你没有引用回音的话。 基本上你这样做:

<input type="text" value=one two three />

但你需要这样做:

<input type="text" value="one two three" />

只需在PHP周围添加“”。

BTW:如果你不想获得XSSed,你需要逃避这些。这些攻击非常可怕。在此处阅读:https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)

答案 1 :(得分:0)

问题是quotes当你在php上做回声时,你需要简单引用它以使其有效。

Html理解twothree<input>的其他参数,因为它未在结果html中引用。

使用这种方式:

<?php 
    Echo "<input type=\"text\" name=\"Address\"     value=\"one two three\"><br>";
?>

<?php 
    $str ="one two three";
    Echo "<input type=\"text\" name=\"Address\"     value=" . $str . "><br>";
?>

<input type="text" name="Address" value='<?php echo "one two three";?>'><br>
<input type="text" name="Address" value='<?php $str = "one two three"; echo $str;?>'><br>
<input type="text" name="Address" value="one two three"><br>

答案 2 :(得分:0)

因为你没有在html的“”或“'标记中设置它。

2. Line:... \“Address \”value =“。$ str。”&gt;
....“; 例如,value属性的输出:value = one two 3 翻译现在认为两个和三个是属性而不是价值的一部分。 与其他案件相同。

答案 3 :(得分:0)

我认为在这个输入类型的value属性中有一个带空格的字符串。 HTML将其视为输入文本的单独属性。

要解决此问题,您可以在开始和结束值属性时使用单引号。 看这个例子。希望你会喜欢这个。我刚刚修改了你的代码。

<?php
   echo "<input type=\"text\" name=\"Address\" value=\"one two three\"><br>";
?>

<?php
   $str ="one two three";
   echo "<input type=\"text\" name=\"Address\" value='" . $str . "'><br>";
?>

<input type="text" name="Address" value='<?php echo "one two three";?>'><br>
<input type="text" name="Address" value='<?php $str = "one two three"; echo $str;?>'><br>
<input type="text" name="Address" value="one two three"><br>

在这种情况下,它将在每个文本框中获得One Two Three。