Reputation: 24375
Basically, I am trying to call a Javascript alert via PHP. Although, the alert does not work at all.
This is my echo statement that dynamically creates the alert
echo "<script>alert('Uploaded file was not in the correct format\n\nExample of the correct format:\nquestion1,answer1,answer2,answer3,answer4\n
question2,answer1,answer2,answer3,answer4\nquestion3,answer1,answer2,answer3,answer4')</script>";
I found that if I change the alert to this, it works perfectly:
echo "<script>alert('Uploaded file was not in the correct format')</script>";
I believe there is a problem with the linebreaks.
I changed my code to this, yet still no luck:
echo "<script>alert('Uploaded file was not in the correct format\\nExample of the correct format:\\nquestion1,answer1,answer2,answer3,answer4\\n
question2,answer1,answer2,answer3,answer4\\nquestion3,answer1,answer2,answer3,answer4')</script>";
I am getting an error of:
SyntaxError: unterminated string literal
[Break On This Error]
alert('Uploaded file was not in the correct format\nExample of the
------^
createplay.php (line 1, col 6)
Does anyone have any suggestions to why this is not working? Searched online far and wide and could not find a solution.
Thanks in advance!
Upvotes: 0
Views: 175
Reputation: 1761
Put one space between \n and next character and it will work. I tried that for your code on my system and it worked
Upvotes: 0
Reputation: 657
To add to Quentin's answer, would switching the single and double quotes around also fix the issue?
Upvotes: 1
Reputation: 1167
Try something like that:
<?
echo "<script>alert('Uploaded file was not in the correct format\\n\\nExample of the correct format:\\nquestion1,answer1,answer2,answer3,answer4\\n
question2,answer1,answer2,answer3,answer4\\nquestion3,answer1,answer2,answer3,answer4')</script>";
?>
Upvotes: 1
Reputation: 943564
Your \n
characters are being replaced by literal line breaks by PHP.
Am unescaped literal line break inside a string literal is an error in JavaScript.
Use \\n
to send a line break escape sequence to JavaScript.
Upvotes: 2