where's my line break...help

Discussion in 'JavaScript' started by php-lover, Jul 12, 2008.

  1. #1
    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.:confused:

    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.
     
    php-lover, Jul 12, 2008 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    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.
     
    rohan_shenoy, Jul 12, 2008 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    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.
     
    php-lover, Jul 12, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    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.
     
    php-lover, Jul 13, 2008 IP