Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate HTML form from SQL Database that includes HTML Markup

Tags:

forms

php

echo

I am pulling information from an SQL database to populate in a form using the PHP Echo command. One of the rows from the sql database includes HTML markup.

When pulling information and populating the form with the data, it will not pull the HTML markup in the form. It looks like this:

Example

Is there a way to have the full HTML markup populate in the form? This is the code I am currently using:

<input name="tag" type="text" id="tag" value="<?php echo $rows['tag']; ?>" size="30"/>
like image 614
Nikolai Avatar asked Aug 19 '26 19:08

Nikolai


2 Answers

Yes, you would need to use htmlentities() to show the entity code (which can be parsed by the browser as plain text) without actually forcing the browser to render the HTML.

echo htmlentities($rows['tag']);
like image 108
Ohgodwhy Avatar answered Aug 21 '26 17:08

Ohgodwhy


You need to escape the html code you want to use for populating the form using either htmlentities() or htmlspecialchars(). (See here)

It is in any case good and I would even say needed to use these functions whenever you echo values from a database or other untrusted source. This to prevent script injection, one of the major pitfalls in web development.

like image 33
Bas van Stein Avatar answered Aug 21 '26 18:08

Bas van Stein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!