繁体   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