简体   繁体   中英

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

I have a message that I would like to show by using alert()

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

This is working but if I add a new line '\\n' into the message

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


it will not show out the pop out message. What is the problem?

Edit it to not change to newline in PHP, instead in javascript:

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

OR by adding extra backslash: "\\\\n" .

According to Panther, also truth: use 'alert("' . $message . '")' .

Add another \\ backslash on your newlines:

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

Output

或者使用 PHP_EOL

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

\\n etc. aren't interpreted in single quotes, just in double quotes.

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

OR

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

You need to use Line feed + Newline

\r\n

and that will work as shown in the following script:

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.");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM