Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2008-11-03
Medium Priority
Last Modified: 2012-05-05
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!
Question by:cfmalik
  • 3
  • 2
LVL 29

Expert Comment

ID: 22874411
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?
LVL 29

Expert Comment

ID: 22874934

Author Comment

ID: 22875120
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?

Author Comment

ID: 22875242
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>

Accepted Solution

cfmalik earned 0 total points
ID: 22875478
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!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question