2013-08-23 54 views
0

基本上,我試圖通過PHP調用Javascript警報。雖然,警報根本不起作用。爲什麼我的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) 

有沒有人有這是爲什麼不工作有什麼建議?在線搜索範圍很廣,找不到解決方案。

在此先感謝!

+3

嘗試使用'\\ n'而不是'\ n' – Pointy

+0

爲什麼你看着你的PHP並聲明你的JavaScript不工作?看看生成的JavaScript! – Quentin

+1

如果交換引用樣式,會發生什麼情況? 'echo'';' – j08691

回答

2

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

字符串文字內部的未轉義文字換行是JavaScript中的錯誤。使用\\n向JavaScript發送換行符轉義序列。

+0

我改變了我的'echo',但仍然拒絕工作'echo'「;' – Fizzix

+0

生成的JS看起來像什麼? JS控制檯說什麼? – Quentin

+0

更新我的問題 – Fizzix

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>"; 
    ?> 
1

爲了增加昆汀的回答,會切換單和雙引號也能夠解決問題?

+0

謝謝你,完美的工作:) – Fizzix

+0

很高興能有一個幫助,我實際上只是試了一下,它不適合我!是雙重逃脫? –

0

在\ n和下一個字符之間放一個空格,它將起作用。我試過你的代碼在我的系統上,它的工作