php - Why do I get instead of a new line getting data from AJAX?
I have a problem: so I have a data thats coming out of the database and stored in a text area. When the user changes the data in the text area, the content is sent to javascript and via AJAX (POST to a PHP script) the database is updated. This works fine until the user starts adding newlines. Then javascript transforms this into a \n-character and thus it gets stored in the database as \n.
What I want is to have actual newlines in my database and not the \n newline-characters. Is there any way that I can use php to replace the \n with an actual newline (NOT a br)? I have tried altering the database field after the edit with the char(10), but for some reason this is not working in the script except when I do it manually in phpmyadmin?
When editting with a full php request, the newline in a text area is correctly stored as a char(10) in mysql, not as \n.
Anyone got a clue?
Answer
Solution:
Store it as it is but escape first with real_escape_string
real_escape_string
converts what is a newline into the 4 character string '\n\r'Answer
Solution:
Use
[nl2br][1]
function to replace/n
with newlineInsert line breaks where newlines (\n) occur in the string:
The browser output of the code above will be: