Search code examples
phphtmlspecialchars

How to allow the user edit input fields that contain html tags


I am using PHP5 and some of the fields in the database contain HTML tags. The user should be able to edit these and save them as part of the form. Let's take a simple example.

I have a field called link that comes from the database and contains an HTML link tags such as:

$link = "<a href=\"http://www.google.com\">go to google</a>";

In my html form I have an input field that will allow the user to edit the $link contents(including tags) - I use Smarty templating engine so hence the {$link} notation

<input name="link" id="link" value="{$link}" />

Issue I have now is that the first time the link field gets displayed as is, so with tags and all, but when the user saves to the database and page refreshes, I see the words "go to google", so the tags get stripped out. Upon closer inspection, that is what is saved in the database as well.

How do I deal with this situation, given the simple example above?


Solution

  • Use the PHP function htmlentities($link) to scape those tags.

    Here is the Manual.