cssyphus
cssyphus

Reputation: 40038

Echo var from PHP into textarea control

Desired outcome: define string var in PHP and echo it into textarea control as contents.

Problem: Textarea only contains:

    </body>
</html>

INDEX.PHP:

<?php
    $y = "Bonkers";
?>
<html>
<body>

    Here is a textarea control:<br />
    <textarea id='myText'  rows="30" cols="120" value='<?php echo $y; ?>' />

</body>
</html>

Upvotes: 1

Views: 25867

Answers (3)

Nagesh87
Nagesh87

Reputation: 9

Do You Know !! You can echo something in textarea by using placeholder attribute also. Code is as follow.

<textarea placeholder="<?php echo $_SESSION['your_name']; ?>"></textarea>

Upvotes: 0

Chris Forrence
Chris Forrence

Reputation: 10094

<textarea> doesn't have an HTML value attribute. Therefore, the correct way to set text within a textarea is as follows:

<textarea id="myText" rows="30" cols="120"><?php echo $y; ?></textarea>

You can access it within JavaScript or jQuery, however, as you would a normal input field.

// jQuery example
$("textarea#myText").val();

The reason why your textarea contains the ending body and html tags is due to how <textarea> is closed; it's only meant to be used as <textarea></textarea>, not <textarea />. (Reference)

Upvotes: 8

federicot
federicot

Reputation: 12341

The textarea element doesn't have a value attribute. This is how you should do it:

<textarea id='myText'  rows="30" cols="120"><?php echo $y; ?></textarea>

Upvotes: 12

Related Questions