Php 文本区域中的占位符文本未正确显示

Php 文本区域中的占位符文本未正确显示,php,html,Php,Html,当我用php变量设置textarea的占位符时,它在空格处被截断 我想你忘了双引号了。试一试 <textarea placeholder="<?php echo htmlentities($comments); ?>" rows="6" cols="35" name="comments" maxlength="20" ></textarea> 您应该使用: placeholder="<?php echo htmlentit

当我用php变量设置textarea的占位符时,它在空格处被截断


我想你忘了双引号了。试一试

<textarea
   placeholder="<?php echo htmlentities($comments); ?>"
   rows="6"
   cols="35"
   name="comments"
   maxlength="20"
></textarea>
您应该使用:

placeholder="<?php echo htmlentities($comments); ?>"
placeholder=“”
如果
$comments
变量包含空格或引号,它们将被转义。
您可以查看htmlentities文档。

问题在于它应该是:

<textarea placeholder="<?php echo $comments; ?>">...

在其他答案中添加一点解释:

占位符
是一个属性。属性值可以不加引号,但如果它包含任何空格,则只有字符串中的第一个单词将被解释为属性值。之后的任何其他单词都将被解释为附加属性。如果查看页面源代码,您将看到整个
$comments
字符串都在那里

<textarea placeholder=word other words></textarea>

看看这里突出显示的语法。注意“其他”和“单词”是如何像“占位符”一样是红色的吗


现在它们是价值的一部分


您只需要将单词组合在一起,这样浏览器就不会认为您给了它额外的属性。

请您的问题将代码添加为文本,而不是图像。您可以使用类似于
{}
的按钮或使用Control+K轻松格式化代码。这有帮助吗?
<textarea placeholder="word" "other" "words">...
<textarea placeholder="word other words">...
<textarea placeholder=word other words></textarea>
<textarea placeholder='word other words'></textarea>
<textarea placeholder="word other words"></textarea>