Reading text with newline from db via php and placing in client javascript variable

R

Randell D.

Folks,

I am cross-posting on purpose, even though my problem appears to be
javascript related there might well be a function unknownst to me that
will help resolve the problem before sending to the client...

Basically I have a textarea box that is saved from client via php into
my mysql database.

When I later try to restore the data I can see it in the HTML source
from my browser (I'm using latest version of Firefox) but an error in
the javascript console tells me the variable is not terminated... This
is because there is a newline character in my text.

I've tried addslashes and htmlentities in php but its not worked for
me - can someone advise me of what the solution/workround is?

(Please reply to the group and not me to help others)

thanks!
 
H

Henry

I am cross-posting on purpose, ...
When I later try to restore the data I can see it in the HTML source
from my browser (I'm using latest version of Firefox) but an error in
the javascript console tells me the variable is not terminated...
<snip>

If your problem is that your server code is inserting line terminator
characters into source code that will be interpreted a on the client
as javascript string literals then there is nothing that you can do
about with javascript on the client. The resulting syntax error in the
javascript is just fatal.

Whenever you inset anything on the server into the source code that
you will be returning to the client you need to (as a matter of
course) 'encode' it appropriately for the context into which you are
insetting it. Be that HTML, XML, javascript string literals or
whatever.

Javascript string literals may not include Line Feed, Carriage Return,
Line Separator (Unicode Hex code point 2028) or Paragraph Separator
(Unicode Hex code point 2029). All of these would need to be
transformed into an escape sequence that javascript will interpret as
the character in question. For LF and CR, "\n" and "\r" respectively,
along with the HEX escape sequences "\x0A" and "\x0D", respectively,
or the Unicode escape sequences "\u000A" and "\000D" respectively, can
be used. For the other two line terminators the escaping can only be
done with the Unicode escape sequences "\u2028" and "\u2029"
respectively (suggesting that a function that did this encoding may
find it easier to transform all problematic characters into the
Unicode escape sequences).

There are another group of characters that will be problematic if they
appear in string literals; backspace, tabs (horizontal and vertical),
form feed, double and single quotes (depending on which is used to
delimit the string literal) and the backslash character because it is
used to introduce all escape sequences (and may combine will all
characters that follow it to form an escape sequence). Thus, for
safety, all of this set should be transformed into equivalent escape
sequences (be they character escape sequences, Hex escape sequences or
Unicode escape sequences) (remembering to do the backslashes first).

Also, do not use the official "\v" as an escape sequence for the
vertical tab character as IE has a bug that leaves it interpreting
that sequnce as a "v" character. Instead use the Hex or Unicode escape
sequences "\x0B" and "\u000B" for consistent results.
 
R

Rik

Folks,

I am cross-posting on purpose, even though my problem appears to be
javascript related there might well be a function unknownst to me that
will help resolve the problem before sending to the client...

Basically I have a textarea box that is saved from client via php into
my mysql database.

When I later try to restore the data I can see it in the HTML source
from my browser (I'm using latest version of Firefox) but an error in
the javascript console tells me the variable is not terminated... This
is because there is a newline character in my text.

I've tried addslashes and htmlentities in php but its not worked for
me - can someone advise me of what the solution/workround is?

$string = str_replace(array("\n","\r"),array('\n','\r'), $string);
 
R

Rik

Rik said the following on 7/26/2007 1:19 PM:

Shouldn't that be:

$string = str_replace(array("\n","\r"),array('\\n','\\r'), $string);

As simply replacing "\n" with '\n' doesn't escape the \ in the newline
sequence which is what is throwing JS as PHP is reading it as a newline
and breaking the line up before being sent to the browser.

Nope, not in PHP: "\n" is a newline character, '\n' is just a literal
slash+n. Double or single quotes make a difference there.
 
R

Randell D.

Nope, not in PHP: "\n" is a newline character, '\n' is just a literal
slash+n. Double or single quotes make a difference there.


Thanks - I never knew that the single or double quotes could make such
a serious difference... and your suggestion worked...

For anyone reading this post in the future they should take great care
to ensure they are using the correct source string - my data returns
from my DB in a multi-dimensional array and I was referencing it
incorrectly which resulted in no output. I had originally assumed the
above solution did not work but then I printed my source data before
and after the result of str_replace and discovered they were both
blank... simple mistake but possible even for the most talented of the
talented :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top