How To Echo Php Variable In An Html Textarea Form


One of the simple yet frustrating challenges you might face when working on core PHP is to echo a php variable inside an HTML textarea form attribute.
How did I even come up with the problem?
So I was working on the admin panel (dashboard) of a client's website while trying to implement the edit feature in which I could edit product information that I had already uploaded to the database. I did that by calling the information using ID into the form before I used the UPDATE syntax.
The problem was that I could call the other information into the form because I could simply use it as value.
<input type="email" name="email" value="<?php echo $text; ?>">
But this doesn't work in textarea because it does not support the value attribute but I was able to bypass it because The defaultValue property sets or returns the default value of a text area. The default value of a text area is the text between the <textarea> and </textarea> tags. I simply wrote my code as this.
<form action="update.php" method="post"> <label for=""></label> <textarea id="" name=""><?php echo $text; ?></textarea> <button type="submit">Submit</button> </form>
I echoed my PhP variable between <textarea></textarea> and it was solved.
Subscribe to my newsletter
Read articles from Modupe Ladele directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Modupe Ladele
Modupe Ladele
Dev + Writer = Devwriter