-
4 Apr 2008 7:40 AM #1
TextArea <br> and newline
TextArea <br> and newline
Hello,
I have read numerous threads related to TextArea and newlines but I haven't found one that describes a solution that has worked for me.
What I want to be able to do is put an instance of TextArea in a FormPanel, have the user enter multiple lines of text in it, save the text in a db on the server (including the newlines), and send the text back to the client at some point and display it in the TextArea instance newlines and all just as it looked when the user entered it.
Presently I escape the newlines in the text on the server before it is sent to the client using JSON (so the text as a javascript string might look like "this\nis\nsome\ntext") but I found out that that doesn't work because the text is displayed as HTML. So next I tried sending the text back with the newlines replaced with "<br />" but just as with the newlines "<br />" was simply displayed.
I'm probably missing something obvious and may have very well failed to enter the correct search terms into the forum search to find what I'm looking for. Others must have tackled this issue 100X over....what am I missing?
-
4 Apr 2008 7:52 AM #2
You escape on the server before sending?
Show us the code you use to do that.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
4 Apr 2008 8:10 AM #3
If it is still showing up with <BR> tags then you arent in fact doing a replace on the server. Firebug can inspect the ajax communication to see for sure.
I typically run the replace regex on the client side when i need to do this.
-
4 Apr 2008 8:12 AM #4
You don't use <br> in a textarea, you use newline characters. The OP probably is not escaping them correctly.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
4 Apr 2008 8:33 AM #5
The server is a device so it's written in C. Here's the code that does the escaping.
Code:#define TRYPUTC(ch) \ { \ if (putc((ch), cgiOut) == EOF) { \ return -1; \ } \ } int jsonEscapeData(char *data, int len) { while (len--) { if(*data == '\"' || *data == '\\') { TRYPUTC('\\'); TRYPUTC(*data); } else if(*data == '\r') { TRYPUTC('\\'); TRYPUTC('\\'); TRYPUTC('r'); } else if(*data == '\n') { TRYPUTC('\\'); TRYPUTC('\\'); TRYPUTC('n'); } else { TRYPUTC(*data); } data++; } return 0; } int jsonEscape(char *s) { return jsonEscapeData(s, (int) strlen(s)); }
-
4 Apr 2008 8:39 AM #6
Animal's right....
It just took that short break of not looking at the code to realize that I had one too many '\' when I escaped the text.
Sorry for the trouble. The least I can do is post the working code.
To clarify where I went wrong...\r and \n are not actually being escaped but instead being replaced by the the distinct characters '\\', 'r', and 'n'.Code:#define TRYPUTC(ch) \ { \ if (putc((ch), cgiOut) == EOF) { \ return -1; \ } \ } int jsonEscapeData(char *data, int len) { while (len--) { if(*data == '\"' || *data == '\\') { TRYPUTC('\\'); TRYPUTC(*data); } else if(*data == '\r') { TRYPUTC('\\'); TRYPUTC('r'); } else if(*data == '\n') { TRYPUTC('\\'); TRYPUTC('n'); } else { TRYPUTC(*data); } data++; } return 0; } int jsonEscape(char *s) { return jsonEscapeData(s, (int) strlen(s)); }
-
9 Mar 2013 5:11 AM #7
you can use this for new line
& # 1 3 ;
no space in between.....
try it.....


Reply With Quote