Page 1 of 1

Storing html in DB and Displaying in TextArea

Posted: Thu Mar 19, 2009 6:53 pm
by tpra21
I am storing html in a mysql DB and then retrieving the html from the DB and displaying it in a textarea of a form. For some reason that I do not understand, my html is being displayed on the webpage as is (not converting to html even though the syntax is correct.

Ex: The data in my DB field is: My name is <b>Adam</b>. When I retrieve the field and echo it to the screen, it still says: My name is <b>Adam</b> instead of Adam being in bold. Below is my code. Thanks in advance for any help or time spent.

Code: Select all

 
if(isset($_POST['profile'])){
 
    $content = $_POST['content'];
 
    // Starts the BBcode.
    $bbcode = array("[u]", "[U]", "[/u]", "[/U]", "[i]", "[I]", "[/i]", "[/I]", "[b]", "[B]", "[/b]", "[/B]", ":)", ":(" , ":D", ":p"); // separate with commas.
    $newbbcode = array("<u>", "<U>", "</u>", "</U>", "<i>", "<I>", "</i>", "</I>", "<b>", "<B>", "</b>", "</B>", "<img src=\"images/smile.gif\">", "<img src=\"images/frown.gif\">", "<img src=\"images/big-smile.gif\">", "<img src=\"images/tongue.gif\">");
 
    $content =  str_replace($bbcode, $newbbcode, $content);
 
    // to block more words just add them in quotes and seperate with commas.
    $badWords = array("<?php", "<?PHP", "?>");
 
    foreach($badWords AS $v) {
        // Replaces the words to nothing.
        $content = addslashes(str_replace($v, "", $content));
        $title = str_replace($v, "", $content);
    }
 
    if(empty($content)){
        echo "<table width=60% class='failed' align='center'>";
        echo "<tr><td align='center'>You forgot to put text in your Personal Profile!</td></tr>";
        echo "</table>";
    }else{
        $newPassword = md5($new1);
        $uUpdate = mysql_query("UPDATE user SET uProfile = '$content' WHERE uUsername = '$username'");
 
        echo "<table width=60% class='success' align='center'>";
        echo "<tr><td align='center'>Personal profile updated successfully!</td></tr>";
        echo "</table>";
    }
 
}
 
$profileQuery = mysql_query("SELECT uProfile FROM user WHERE uUsername = '$username'");
$profileRow   = mysql_fetch_array($profileQuery);
?>
 
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<table width = 60% cellpadding=5 cellspacing=0 class='outline' align='center'>
<tr class='header'><td colspan=3 align='left'>Personal Profile</td></tr>
<tr class = 'detail_color'>
  <td align='center' width = 60% colspan=3><textarea name="content" style="width: 95%;height: 200px;"><? echo stripslashes($profileRow['uProfile']); ?></textarea></td>
</tr>
<tr class = 'detail_color'>
  <td align='center' width = 20% colspan=3><input type="submit" style="width: 95%;" name='profile' value="Save Profile"></td>
</tr>
</table>
</form>
 

Re: Storing html in DB and Displaying in TextArea

Posted: Fri Mar 20, 2009 1:43 am
by califdon
Text areas do not parse html. What you see is what you get. If you want to parse html (interpret the tags), you have to do it outside <textarea>s or <input>s. You could put it within a <div>, for example.

Re: Storing html in DB and Displaying in TextArea

Posted: Fri Mar 20, 2009 11:02 am
by tpra21
Thanks for the response! I am at work, but will try using a division as soon as I get home.

One question though; how do I use the <div> to replace the text area, yet allow the user to enter data into the exact same spot for updating/changing (assuming if I place the html wrapped in the division tag in the text area control, I will receive the same result as I am now)?

thanks again - Adam

Re: Storing html in DB and Displaying in TextArea

Posted: Fri Mar 20, 2009 11:21 am
by califdon
tpra21 wrote:Thanks for the response! I am at work, but will try using a division as soon as I get home.

One question though; how do I use the <div> to replace the text area, yet allow the user to enter data into the exact same spot for updating/changing (assuming if I place the html wrapped in the division tag in the text area control, I will receive the same result as I am now)?

thanks again - Adam
I guess you could try using Javascript to get the InnerHtml property of the <div> and assign it to a hidden <input> element. I haven't ever needed to do that, but I think it would work. You could do that before submitting the form.

Re: Storing html in DB and Displaying in TextArea

Posted: Fri Mar 20, 2009 3:00 pm
by php_east
you might want to take a look at some simple html editors. typically, they replace the textareas with a frame layer and does all the javascripts. and then it provides you with a simple way to get the textarea contents from their javascript such as $editor->getHTML();

additionally, from my own experience, the textarea itself is typically also updated, so you could actualy just save the textarea as is. the simple editors are fast loading and could do decent html editing and are fairly easy to use/implement.

hope this helps.