HOW TO ECHO PHP VARIABLE IN AN HTML TEXTAREA FORM

Modupe Ladele
1 min readDec 9, 2023

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 to echo PHP variable in an Html Textarea Form

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.

--

--

Modupe Ladele

This user does have a bio; Animal Scientist, apparently I am a teacher. Ehm... Researcher