簡體   English   中英

php回顯帶有新行'\\n'的警報消息不起作用

[英]php echo alert message with new line '\n' not working

我有一條消息要使用 alert() 顯示

$message = "No result found for the following info:Name: ".$FullName."IC: ".$ID." in database.";
echo "<script>alert('".$message."'); window.history.back();</script>";

這是有效的,但如果我在消息中添加一個新行 '\\n'

$message = "No result found for the following info:\nName: ".$FullName."\nIC: ".$ID." in database.";


它不會顯示彈出消息。 問題是什么?

編輯它不要在 PHP 中更改為換行符,而是在 javascript 中:

'No result found for the following info:\nName: '.$FullName.'\nIC: '.$ID.' in database.'
^                                               ^           ^      ^     ^             ^

或者通過添加額外的反斜杠: "\\\\n"

根據 Panther 的說法,也是事實:使用'alert("' . $message . '")'

在換行符上添加另一個\\反斜杠:

$message = "No result found for the following info:\\nName: ".$FullName."\\nIC: ".$ID." in database.";

輸出

或者使用 PHP_EOL

 $msg = 'Hello' . PHP_EOL . 'Next line';

\\n等不解釋為單引號,只解釋為雙引號。

echo "<script>alert(\"" . $message . "\"); window.history.back();</script>";

或者

echo '<script>alert("' . $message . '"); window.history.back();</script>';

你需要使用換行+換行

\r\n

這將如以下腳本所示工作:

echo "If you view the source of output frame \r\n you will find a newline in this string.";
echo nl2br("You will find the \n newlines in this string \r\n on the browser window.");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM