Home » questions » Help with JavaScript?

Help with JavaScript?

2006-07-30 00:31:38, Category: Programming & Design
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

Answers

  1. tedjn

    On 2006-07-30 03:19:00


    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.