基本上,我正在尝试通过PHP调用Javascript警报。不过,警报根本不起作用。
这是我的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>";
我发现,如果我把警报改为这个,它会完美地工作:
echo "<script>alert('Uploaded file was not in the correct format')</script>";
我认为断线有问题。
我把代码改成了这个,但仍然没有运气:
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>";
我得到一个错误:
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)
有人对为什么这不起作用有什么建议吗?在网上广泛搜索,找不到解决方案。
提前感谢!
您的'n
字符正在被PHP用文字换行符替换。
在JavaScript中,字符串中的换行是一个错误。
使用''n
向JavaScript发送换行转义序列。
试试这样的东西:
<?
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>";
?>
要补充Quentin的答案,切换单引号和双引号是否也能解决问题?
在''n和下一个字符之间放一个空格,它就会起作用。我在我的系统上为您的代码尝试过,它在上运行