[英]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.