Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Alert box line breaks

    This is easy in Javascript, but somehow it is not recognized in PHP. I want to display an alert message but break up the message with line breaks. It works without the \n, but errors out when I include the \n.

    The error is "unterminated string constant".

    What is wrong with my syntax?

    This works...
    Code:
    if (isset($_SESSION['error_messages'])) {
    $error_messages = $_SESSION['error_messages'];
    print "<script>alert('Errors were found!' + '$error_messages')</script>";
    }
    This does not work...
    Code:
    // Retreive error messages //
    if (isset($_SESSION['error_messages'])) {
    $error_messages = $_SESSION['error_messages'];
    print "<script>alert('Errors were found!' + '\n' + '$error_messages')</script>";
    }
    This does not work either...
    Code:
    if (isset($_SESSION['error_messages'])) {
    $error_messages = $_SESSION['error_messages'];
    $m_message = "Errors were found!\n$error_messages";
    echo "<script>alert('$m_message')</script>";
    }

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Edit: Guy below me is correct, overlooked that
    Last edited by Iszak; 05-06-2009 at 09:01 PM.

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Your problem is that PHP converts the \n to a line break before JS even gets it. Escape it and all will be well:

    PHP Code:
    print "<script>alert('Errors were found!' + '\\n' + '$error_messages')</script>"
    Alternatively, you could use single quotes in order to make PHP not care:

    PHP Code:
    print '<script>alert("Errors were found!" + "\n" + "'.$error_messages.'")</script>'

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Location
    Atlanta, GA
    Posts
    171
    Thanks
    5
    Thanked 0 Times in 0 Posts
    That worked! Thank you very much!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •