-1

I have a simple contact form with basic php validation, here's the relevant part of code:

    $err = '';

    if(!preg_match("/^[a-zA-Z ]*$/",$name)) {
        $err .= "Name : Only letters and white space allowed.\n";
    }
    if(strlen($name)<3){
        $err .= "Name : Please enter your full name.\n";
    }
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $err .= "Email : Invalid email format.\n";
    }
    if(strlen($email)<8){
        $err .= "Email : Okay, how about using a correct email address?\n";
    }
    if(strlen($message)<30){
        $err .= "Message : Message too short.\n";
    }

    if($err != ''){
        $err = 'There were few errors : \n '.$err;

        //THE ALERT BELOW DOESN'T WORKS, gets added into body code, but does not popup
        echo '<script type="text/javascript">alert("'.$err.'");</script>';

        // While this one works well
        //echo '<script type="text/javascript">alert("This alert works. \nThe one above does not.");</script>';
    }
    else{
        //whatever
    }

The problem is that the alert() in the last if condition doesn't work, it gets added in body code but doesn't pop-up. While if I use normal text instead of $err variable (like the one commented out)- that works all well.

I have had this problem several times in past, I never understood why it happens & what's the solution, I always rather removed the alert and displayed response message normally in a div or something, but this time I need to know - why??

Echoes
  • 324
  • 4
  • 14
  • First of all, go check the browser console, it will tell you about your JavaScript syntax error. After that, go read up on what syntax JavaScript allows for text literals/strings. – CBroe Jun 13 '17 at 13:17
  • I think the issue lies with the newline in php `\n`. I could see this breaking the JS output. – evolutionxbox Jun 13 '17 at 13:18
  • @evolutionxbox I thought so, but it's not, because the alert below that (the one commented) works well. – Echoes Jun 13 '17 at 13:20
  • @CBroe I did check console before posting, yes it shows `Uncaught SyntaxError: Invalid or unexpected token` error, but I am not using any illegal character... that is the question, avoid rushing for downvote without understanding the question. – Echoes Jun 13 '17 at 13:31
  • _"I did check console before posting"_ - no one can know that, if _you_ fail to mention it. _"avoid rushing downvote without understanding the question"_ - I understood your question perfectly well; that’s why I asked you to go read up on the very basics of the JS syntax. – CBroe Jun 13 '17 at 13:36
  • @CBroe Thanks for the help. – Echoes Jun 13 '17 at 13:42

2 Answers2

1

Change New lines to this

$err .= "Name : Please enter your full name.\\n";

Ref: Cant add new lines in Javascript Alert Box

rbaskam
  • 749
  • 7
  • 22
0
if($err != ''){
        $err = 'There were few errors : \n '.$err;


        echo '<script type="text/javascript">alert('.$err.');</script>';
    }
    else{
        //whatever
    }
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 13 '17 at 16:34