0

基本上,我试图通过 PHP 调用 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>";

我发现如果我将警报更改为此,它会完美运行:

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)

有人对为什么这不起作用有任何建议吗?在网上搜索了很多,找不到解决方案。

提前致谢!

4

4 回答 4

2

您的\n字符正在被 PHP 的文字换行符替换。

字符串文字中未转义的文字换行符是 JavaScript 中的错误。

用于\\n向 JavaScript 发送换行符转义序列。

于 2013-08-23T14:20:34.903 回答
1

尝试这样的事情:

    <?
    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>";
    ?>
于 2013-08-23T14:22:48.907 回答
1

为了补充昆汀的答案,切换单引号和双引号也可以解决问题吗?

于 2013-08-23T14:28:41.040 回答
0

在 \n 和下一个字符之间放置一个空格,它将起作用。我在我的系统上尝试了你的代码并且它有效

于 2013-08-23T14:34:07.790 回答