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