When I set the placeholder of my textarea with a php variable, it get cuts off at the space.

- 35
- 10
-
Please [edit] your question to add your code as text, not as an image. You can easily format your code by using the button that looks like `{ }` or by using Control + K. – grooveplex Feb 08 '19 at 23:52
4 Answers
I think you forget double quote. Try
<textarea
placeholder="<?php echo htmlentities($comments); ?>"
rows="6"
cols="35"
name="comments"
maxlength="20"
></textarea>

- 11,075
- 4
- 33
- 54
Just a little explanation to add to the other answers:
placeholder
is an attribute. The attribute value can be unquoted, but if it contains any space, only the first word in the string will be interpreted as the attribute value. Any other words after that will be interpreted as additional attributes. If you view the page source, you'll see that the entire $comments
string is there.
<textarea placeholder=word other words></textarea>
Look at the syntax highlighting here. Notice how "other" and "words" are red like "placeholder"?
<textarea placeholder='word other words'></textarea>
<textarea placeholder="word other words"></textarea>
Now they're part of the value.
Either single quotes or double quotes are fine. You just need something to group the words together so the browser won't think you're giving it extra attributes.

- 41,125
- 10
- 61
- 80
The problem is it should be:
<textarea placeholder="<?php echo $comments; ?>">...
It looks like someone wanted me to explain. To understand what is going on here is that you are mixing two languages. On one layer there is HTML. Before it is sent to the browser it is sent through a PHP processor. When this is processed it will look like this on the original question without the quotes and before the browser does the parsing.
<textarea placeholder=word other words>...
The browser auto quotes the HTML and interprets it as
<textarea placeholder="word" "other" "words">...
So this is why you need to add the quotes around the PHP code. And this is why it is only showing the first value. When it has quotes the browser will lump all the spaced elements in the string as the placeholder's value.
<textarea placeholder="word other words">...
Let me know if there needs to be more clarification or if there is a better way to explaining it.

- 166
- 8