English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hello everyone, I am stuck in one of the javascripts, well actually AJAX bit i guess.
In PHP i have a textarea, I am saving that into the MySQL database as it is using AJAX.

When I am reading it back, I am converting the carriage returns into
to make sure that the enter keys pressed should be displayed as it is, the following is the code:

$change = array("\r\n" => "
");
$string_message = strtr($row['document_text'], $change);
echo $string_message;

This code works fine if I am not using AJAX.

What actually is happening, once the data passes through AJAX and reaches to the php script on the server, all \r\n are converted to null. See the example below.

The text is:

line1
line2
line3

once saved in DB, even using the above replace code, the string looks like this:

line1line2line3

but before saving, i view the string using alert() function in javascript, it shows the string like this:
line1
line2
line3,
but again after reading from DB
line1line2line3

2006-07-29 20:31:38 · 2 answers · asked by Manish 5 in Computers & Internet Programming & Design

2 answers

Let's see. First of all, you may want to check out the nl2br() PHP function [see first source] :-)

Secondly, you most likely do not need to convert \r\n; \n would be fine. I am not too sure how this works in browsers, but I am on Windows and even if there is an extra \r involved, it works fine just working with the \n.

Now, for the crux of your problem, you should probably escape your data before putting it into the DB. Make sure that you are using either addslashes() (and when extracting, stripslashes(), as long as magic_quotes_gpc is not set, which it shouldn't be), or more preferably, mysql_real_escape_string(), although the latter requires an established mysql connection (but that's a given). For more information on usage, just check the PHP manual.

2006-07-29 23:19:00 · answer #1 · answered by tedjn 3 · 1 0

I even do no longer recognize, yet i'm also searching for a fashion to characteristic a "remark" on an internet site because i'm planning to make my own weblog. i recognize worry-free html yet i don't like guestbooks, and in the present day i virtually gave up on Livejournal; they have terrible themes! goodluck to you. if worst is composed of worse, i can't entertain "comments" on my information superhighway website. *sigh*

2016-11-26 23:19:39 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers