Home >Backend Development >PHP Tutorial >Using JS to pop up garbled error messages in PHP

Using JS to pop up garbled error messages in PHP

WBOY
WBOYOriginal
2016-08-08 09:19:311106browse

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;

In this way, when the error message pops up, garbled characters will no longer appear.

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn