Actually, I'm writing my script to send an email through AJAX and PHP script. I use Javascript to retrieve the email message from a textarea on my email form. The problem is when the email sent, all the new line \n characters is loss and there is no line break in the email message. How can I retrieve the email message from the textarea without loosing the \n characters in Javascript. here's how I retrieve my email message. var msg = document.getElementById('msg').value; Your help will much appreciate. Thanks.
How are you sending the email? HTML format or plain text format? If in HTML format, use the nl2br() function.(will convert \n to <br />) If in plain text format, try using \r\n instead of \n.
Thank you rohan_shenoy. Sorry, I forgot to mention that. I'm using plain text email format. I'm retrieved the email message straight from textarea by using the following javascript line: var msg = document.getElementById('msg').value; I send this msg through AJAX connection to a php script for the send mail process, unfortunately, when I check the email message, there's no line break at all. I check all my php code and they are okay. I think the problem occurs when javascript retrieve directly the email message from the textarea. Any ideas.
I found the solution for my problem. Here's the javascript function I use encodeURI(), it might help someone. var msg = document.getElementById('msg').value; msg = encodeURI(msg); //<------important to encode the text first before send to my php script in order to keep the line break. cheers.