...

View Full Version : Saving textarea and display like HTML



Squall Leonhart
11-24-2003, 10:20 PM
Hi, guys.
I have question again.
Please take a look at code.


code:--------------------------------------------------------------------------------
<HTML>
<HEAD>
</HEAD>
<BODY LEFTMARGIN=0 MARGINWIDTH="0" MARGINHEIGHT="0">
<form name="frmAdd" method="post" action="updateMSI.asp?state=add" onsubmit="return add();">
<table border="0" cellpadding="3" cellspacing="1" width="100%" class="forumline">
<tr>
<td class="row1" valign="top"><span class="gen"><b>Content</b></span></td>
<td class="row2" valign="top"><span class="gen"> <span class="genmed"><textarea type="text" rows="15" cols="35" wrap="virtual" style="width:450px" tabindex="3" name="content"></textarea></span></td>
</tr>
<tr>
<td class="catBottom" colspan="2" align="center" height="28"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</BODY>
</HTML>
--------------------------------------------------------------------------------

As you can see, when I type following text in text area

quote:
--------------------------------------------------------------------------------

Hi Team:

This morning we released our financial results for the third quarter of 2003 (please see attached News Release or check out the Wins Bulletin Board on each of your floors).

Last quarter we discussed going for a Othree-peatP and we are pleased to announce that we did it!


--------------------------------------------------------------------------------


And click submit and it saves on the database alright.
But problem is when I get this text from databse to display on following page.


code:--------------------------------------------------------------------------------
<html>
<head>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
dim todaysDate
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
num=Request.QueryString("ID")

strSQL = "SELECT * FROM tblMSI WHERE ID =" & num
rsGuestbook.Open strSQL, adoCon
todaysDate=rsGuestbook("Date")
%>
<table width="90%" border="1" bordercolor="#666666" align="center" cellpadding=20>
<tr>
<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">
<% Response.Write (rsGuestbook("Content")) %>
</td>
</tr>
</table>
<%
'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html>
--------------------------------------------------------------------------------

Text appears like this!

quote:
--------------------------------------------------------------------------------

Hi Team: This morning we released our financial results for the third quarter of 2003 (please see attached News Release or check out the Wins Bulletin Board on each of your floors).
--------------------------------------------------------------------------------



How can I make it display like same as when I entered it?
Just like this forum.

r0ck1t
11-25-2003, 02:51 AM
Get the functions used in a forum where they insert those special tags and the when submitting the convert these to html tags. Then when you write the tex tin the db to the browser it will interpret the html tags and your formatting will be intact.

Download a free ASP forum and find their formatiing functions.

glenngv
11-25-2003, 08:16 AM
Add the <pre> tag to preserve linefeeds

<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">
<pre>
<% Response.Write (rsGuestbook("Content")) %>
</pre>
</td>

The default font for <pre> tags is Courier, just change it by defining a CSS style for it.

oracleguy
11-25-2003, 07:19 PM
Wouldn't Server.HTMLEncode work too?

Squall Leonhart
11-25-2003, 09:21 PM
Wouldn't Server.HTMLEncode work too?
It doesn't work.:(

whammy
11-26-2003, 12:50 AM
If you want to preserve line breaks that were entered in a textarea, but want them to display in HTML, you'll need to use something like this:


Function VbCrLfToBreak(ByVal str)
If IsNull(str) Then Exit Function
VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
End Function

glenngv
11-26-2003, 05:16 AM
whammy's code should work, and mine too.

jeskel
11-26-2003, 09:14 AM
would you use it when recording or when displaying?

whammy
11-26-2003, 02:09 PM
Displaying. :)

Oakendin
11-26-2003, 02:40 PM
Here's a function in Jscript using regExp.



// This prints out text with html breaks
function textToHtml(text) {
// replace \n with <br>
text = text.replace(/\n/g, "<br>");
return text;
}

jeskel
11-26-2003, 03:40 PM
cool guys... useful thread for me as well :)

Squall Leonhart
11-26-2003, 05:26 PM
So I entered code like this


<html>
<head>
<script language="JavaScript" type="text/javascript">
function textToHtml(text) {
// replace \n with <br>
text = text.replace(/\n/g, "<br>");
return text;
}
</script>

</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
dim todaysDate
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
num=Request.QueryString("ID")

strSQL = "SELECT * FROM tblMSI WHERE ID =" & num
rsGuestbook.Open strSQL, adoCon
todaysDate=rsGuestbook("Date")
%>
<table width="90%" border="1" bordercolor="#666666" align="center" cellpadding=20>
<tr>
<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">
<% Response.Write (rsGuestbook("Content")) %>
</td>
</tr>
</table>
<%
'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html>

It seems words are sticking together still.....
Do you know which part need to be fixed to utilize that script?

Oakendin
11-26-2003, 06:15 PM
Try the stuff in red:
-----


<html>
<head>
<script language="JavaScript" type="text/javascript">
function textToHtml(text) {
// replace \n with <br>
text = text.replace(/\n/g, "<br>");
return text;
}
</script>

</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
dim todaysDate
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
num=Request.QueryString("ID")

strSQL = "SELECT * FROM tblMSI WHERE ID =" & num
rsGuestbook.Open strSQL, adoCon
todaysDate=rsGuestbook("Date")
%>
<table width="90%" border="1" bordercolor="#666666" align="center" cellpadding=20>
<tr>
<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">

<script language="JavaScript" type="text/javascript">
document.write(textToHtml(<%=(rsGuestbook("Content"))%>);
</script>

</td>
</tr>
</table>
<%
'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html>

Oakendin
11-26-2003, 06:18 PM
This is the VBScript version, use the stuff in red:
-----


<html>
<head>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
Function VbCrLfToBreak(ByVal str)
If IsNull(str) Then Exit Function
VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
End Function

dim todaysDate
Dim adoCon 'Holds the Database Connection Object
Dim rsGuestbook 'Holds the recordset for the records in the database
Dim strSQL 'Holds the SQL query for the database

Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
num=Request.QueryString("ID")

strSQL = "SELECT * FROM tblMSI WHERE ID =" & num
rsGuestbook.Open strSQL, adoCon
todaysDate=rsGuestbook("Date")
%>
<table width="90%" border="1" bordercolor="#666666" align="center" cellpadding=20>
<tr>
<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">
<% Response.Write VbCrLfToBreak((rsGuestbook("Content"))) %>
</td>
</tr>
</table>
<%
'Reset server objects
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html>

jeskel
11-26-2003, 06:43 PM
hey Oakendin... You are pretty handy. You should hang around here more often :)

Squall Leonhart
11-26-2003, 07:19 PM
Thank you.
Oakendin, I had written the code above.
But error message appeared


Microsoft VBScript runtime error '800a005e'

Invalid use of Null: 'Replace'

I don't know much about vbscript.
Is it some kind of Syntax error?:confused:

Oakendin
11-26-2003, 09:42 PM
I changed the VBScript function in red. Whammy's function does a null check, if null, exit the function. I modified it to do something if not null.

Check out these sites for my ASP-VBScript tricks and tips:
http://www.w3schools.com/asp/default.asp
http://www.haneng.com/
----


<%@ Language=VbScript %>
<html>
<head>
<title>Test ASP</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<%
Function VbCrLfToBreak(ByVal str)
If NOT IsNull(str) then
VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
End If
End Function

dim someNoneFormattedText
someNoneFormattedText = " some " & vbCrLf & " unformatted " & vbCrLf & " text "

dim emptyString
emptyString = null
%>
<table width="90%" border="1" bordercolor="#666666" align="center" cellpadding=20>
<tr>
<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">
<b>Did not use function</b><br>
<% Response.Write someNoneFormattedText %>
<br><br>
<b>Uses function</b><br>
<% Response.Write VbCrLfToBreak(someNoneFormattedText) %>
<br><br>
<b>Did not use function</b><br>
<% Response.Write emptyString %>
<br><br>
<b>Uses function</b><br>
<% Response.Write VbCrLfToBreak(emptyString) %>
</td>
</tr>
</table>

</body>
</html>


-----
Thanks bouchel for the kind words. Just trying to give something back since I've gotten a lot from these forums.

jeskel
11-26-2003, 11:55 PM
:)

glenngv
11-27-2003, 01:20 AM
Originally posted by Oakendin
Try the stuff in red:
-----


<html>
<head>
<script language="JavaScript" type="text/javascript">
function textToHtml(text) {
// replace \n with <br>
text = text.replace(/\n/g, "<br>");
return text;
}
</script>

...


<script language="JavaScript" type="text/javascript">
document.write(textToHtml(<%=(rsGuestbook("Content"))%>);
</script>


...

</body>
</html>


There should have been quotes in the textToHtml() parameter but then that would still throw an unterminated string error because rsGuestbook("Content") can have linefeeds in it.

document.write(textToHtml("<%=(rsGuestbook("Content"))%>");

I still think my solution is the most efficient because it doesn't require additional processing like replacing linefeeds with <br>s.

<td bgcolor="#CCCCCC" height="300" bordercolor="#666666">
<pre style="font:normal 12px Verdana">
<% = rsGuestbook("Content") %>
</pre>
</td>

jeskel
11-27-2003, 10:33 AM
is the <pre> tag supported by every browser? And every version? I've seen it only a very few times.. That's why I'm rather doing that kind of things server-side... But I wish that I can use <pre>... so much easier :)

glenngv
11-27-2003, 10:51 AM
You've seen it very few times?? This forum uses <pre> when we post
tags :p

http://www.devguru.com/Technologies/html/quickref/html_pre.html

jeskel
11-27-2003, 05:34 PM
oh don't worry... I'm having a hard time with lack of sleep :)... it already started before my first post here... check this if you wanna have fun: http://www.codingforums.com/showthread.php?s=&threadid=29493 :p
Anyways, thanx for the answer!!! :)

whammy
11-28-2003, 05:10 AM
P.S. If you're passing a null string to the function, then you aren't getting a value for your string... the function should be fine as is. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum