Display HTML formatted text in div after retrieving from DB via AJAX

Hi Experts,

I've probably done the hardest part of my spec but mind's boggled by something very strange. I'll explain:

I have a simple cfm page with a div and a textarea, plus AJAX code. The idea is when a user type something into the text area and presses 'add' it does so through AJAX, and then displays the results in the div's innerhtml.

All this works fine.

The problem is when i update div with a new note, i want to add some HTML (<br> & <font>) around parts of the text.

This gets updated to the database fine but when i get it back and display it in the div, it displays it with all the HTML tags around it! ie: <br> will be displayed as <br> not an actual line break!

The weird thing is, when i run this on my machine at home, using 'longtext' data type in MYSQL it works fine. But when i run it at work, storing the data in a 'ntext' field on MSSQL, it displays the HTML code!

Please can some one tell me how to ensure the HTML returned from the db is displayed as HTML?

When i put <pre> tags around it, it just displays as pure text!

Do i need to encode/decode? serialize/deserialize?

Please give code as examples and tell me hy it would work at hme but not work?

Thanks a million!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

when you view the html source are you seeing <br> or are you seeing & lt; br & gt;  ?What is actually stored in the DB?Is ntext not binary? I have no idea if this would have affect on your data (in relation to your problem). Have you tried nvarchar or even varchar?
cfmalikAuthor Commented:
this is how it is saving in db:
hiii (Modified by: Test Comms on 3/11/2008 17:10:43) <br>hi again (Modified by: Test Comms on 3/11/2008 17:11:4)

and this is how it is also displaying on the page, not html escaped (& lt; br & gt;)

How can i ensure the <br> is actually a line break?
cfmalikAuthor Commented:
sorry, just checked again and it saves in db as & lt; br & gt;

but when the variable is put in between <cfoutput> tags, it shows <br>
cfmalikAuthor Commented:
Dont worry guys, found my answer:

as i said, it was escaping the html tags and storing them in db, when brought back from db, i just did the following to the variable;

<cfset tempnotes = "#Replace(rs_Notes,"<","<","All")#">
<cfset tempnotes = "#Replace(tempnotes,">",">","All")#">

and this to the ajax response text;

      var reponsetxt = oXmlHttp.responseText;
      reponsetxt = reponsetxt.replace(/</g, "<");
      reponsetxt = reponsetxt.replace(/>/g, ">");

This has fixed it!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.