Problem description: Submit data from the page to the background (non-Ajax submission). When checking the legality of the data, if an error occurs, echo a piece of JS code to provide an error prompt. As shown below
echo "<script>alert('提交信息有误');</script>";
the pop-up box will appear garbled.
Problem analysis: If the browser is set to automatically detect encoding, garbled characters will not appear. Otherwise, garbled characters may appear. Because the JS code in the script tag is streaked and has no encoding restrictions, garbled characters will appear.
Problem solution: Wrap a layer of HTML code outside the pop-up error message. As follows
$output=<<<EOF <html> <head> <meta http-equiv="Content-Type" c/html;charset=utf-8"> <script>alert("$errMsg");</script> </head> </html> EOF; echo $output;
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the use of JS in PHP to pop up garbled error messages, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.