PDA

View Full Version : String Encoding data?????


Dean
01-10-2003, 10:58 AM
Hi There

I am trying to work out how to get a form to submit the contents of a text area into sql2000 as it is typed in so that it keeps it format.

I Know this means means using the encode string function so that it converts the whitespaces and carriage returns into numeric values.

I Have no idea how to do this.
I can make forms submit and display no problem but at present i am having to insert html code to get data out of the database to appear in the correct format.

can anybody show how to do this or give me an example of the code i need to insert and where it goes. as i find it easier understand code when i see it in place on a page.

any help welcome:confused: :confused:

raf
01-10-2003, 11:16 AM
are you looking for something like this
----------------------------------------------------------------------------
URLEncode
The URLEncode method applies URL encoding rules, including escape characters, to a specified string.

Syntax
Server.URLEncode( string )

Parameters
string
Specifies the string to encode.
Example
The following script

<%Response.Write(Server.URLEncode("http://www.microsoft.com")) %>

produces the output

http%3A%2F%2Fwww%2Emicrosoft%2Ecom

Applies To
Server Object
---------------------------------------------------

the oposit is

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

HTMLEncode
The HTMLEncode method applies HTML encoding to a specified string.

Syntax
Server.HTMLEncode( string )

Parameters
string
Specifies the string to encode.
Example
The following script

<%= Server.HTMLEncode("The paragraph tag: <P>") %>

produces the output

The paragraph tag: &lt;P&gt;

Note The preceding output will be displayed by a Web browser as

The paragraph tag: <P>

If you view source, or open the page as a text file, you will be able to see the encoded HTML.

Applies To
Server Object

Dean
01-10-2003, 11:25 AM
hi there

what i want to be able to do is copy and paste half a page of text and paste it into a box and submit it to the database then when i pull the record back out of the database it appears.

i have put a page below with a single text area that submits to the database. then returns to it's self and displays the new data below the form.

if you could put what you mean on the page so i can see where it goes on the page it will make more sence to me.

:confused:

raf
01-10-2003, 12:52 PM
I'm not sure i understand what you're trying to do, but i believe you want to save text + display it the way you saved it ?
well, just save it. The only thing to take care of, are single quotes (because the sql statement will error if your text contains any)
so you should replace them like this

sql="insert etc etc")
sql=replace(sql,"'","''")

the ' is replaced by two ', but in the database, you'll only insert one ' (trust me)

To prevent weird things when displaying the text, just use the server.HTMLencode stuff.
It'll print the text, like you see it on the page.

Imagen someone types somethig like this in your textbox:
<script>some code that hacks your app </script>

without the server.HTMLencode, the parser could write it to the page like that, and the browser could execute it like that.

if you use htmlencode, the text is just displayed like it is displayed in the database (witch should also be the same as what whas in the textbox. (because the code woun't look like that, since all the weird stuff like <> etc is transformed in HTML code)


i edited your code, but can't test it right now.sorry



edit : something went wrong with previous attachement

Leeus
01-10-2003, 04:09 PM
I had a similar problem with character returns so id used,

string = replace(string, vbcrlf, "<br>")
This preserved the way it looked when it came out of the SQL table.

whammy
01-11-2003, 12:11 AM
The last two posts are absolutely correct.

You can save whatever you want to the SQL Server 2000 database, the only character that will give you a problem is a single quote - and the fix was not only answered above by raf, but is also on one of the three sticky threads at the top of this forum, in "Single quotes give me a syntax error!". :)

Now, when you're pulling the data back out, the latter post by Leeus is also absolutely correct... if you aren't actually copying HTML code with line breaks hard coded as "<br />", then when you pull the text out, you'll need to replace line breaks with "<br />" as demonstrated above, or in the function I use (which is the same thing, but I have a habit of using xHTML compatible code, that's why the <br />)...


Function VbCrLfToBreak(byVal str)
VbCrLfToBreak = Replace(str,vbCrLf,"<br />")
End Function


for more useful functions like this check out

http://www.solidscripts.com/displayscript.asp?sid=10

P.S. As raf was saying, Server.HTMLEncode() is a very important and useful built-in function in ASP (and also ASP.NET!).

What's truly surprising is not many ASP developers know what it does, or how to use it. I managed to figure it out on my own with some research a few months back.

After you fix this (which should be simple now), I would definitely read up (a google search should do the trick) on Server.HTMLEncode(), because it can save you a lot of headaches. I'm still trying to get all of the developers I work with up to speed on what it does and why they need to use it. :)