[英]Why is my Javascript alert not being displayed?
Basically, I am trying to call a Javascript alert via PHP. 基本上,我试图通过PHP调用Javascript警报。 Although, the alert does not work at all.
虽然,警报根本不起作用。
This is my echo statement that dynamically creates the alert 这是我的echo语句,可动态创建警报
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! 提前致谢!
Your \\n
characters are being replaced by literal line breaks by PHP. 您的
\\n
字符已被PHP的文字换行符代替。
Am unescaped literal line break inside a string literal is an error in JavaScript. 字符串文字中未转义的文字换行符是JavaScript中的错误。
Use \\\\n
to send a line break escape sequence to JavaScript. 使用
\\\\n
将换行符转义序列发送到JavaScript。
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>";
?>
为了增加昆汀的答案,将单引号和双引号切换也能解决问题吗?
Put one space between \\n and next character and it will work. 在\\ n和下一个字符之间插入一个空格,它将起作用。 I tried that for your code on my system and it worked
我在您的系统上为您的代码尝试过,并且有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.