Home > Web Front-end > JS Tutorial > What should I do if javascript transmits Chinese garbled characters to PHP?

What should I do if javascript transmits Chinese garbled characters to PHP?

coldplay.xixi
Release: 2020-08-12 10:05:38
Original
1662 people have browsed it

The solution for JavaScript to transmit Chinese garbled characters to PHP: First, use the [encodeURIComponent()] function in the JavaScript code to process the Chinese string; then ensure that the value encoding between JavaScript and back-end programs such as Asp and Php is unified. Can.

What should I do if javascript transmits Chinese garbled characters to PHP?

Solution for JavaScript to pass Chinese garbled characters to PHP:

Use encodeURIComponent() function in javascript code to process Chinese string,

JS code:

<script type=”text/javascript”>
string = encodeURIComponent(string);
location.href = index.php?keyword=’+string;
</script>
Copy after login

Related learning recommendations: javascript video tutorial

PHP code:

<?php
$keyword = (isset($_GET[&#39;keyword&#39;])) ? $_GET[&#39;keyword&#39;] : &#39;&#39;;
?>
Copy after login

Related learning recommendations: php programming (Video)

Ensure that the value encoding between JavaScript and back-end programs such as Asp and Php is unified

On pages with non-English character sets, if you use Ajax for data interaction, you must pay attention to ensuring that the front-end and back-end data are uniformly encoded, otherwise, garbled characters will easily appear!

When the back-end is an ASP program, the following function can be used to maintain the unified encoding of values ​​transferred between the front-end Javascript and Asp:

Encoding: escape(string)

Decoding: (string)

These two functions exist in JavaScript and Asp, and their functions are exactly the same. As long as either end transmits data to the other end, escape is used to encode it first, and the receiving end Use decoding to ensure that there will be no garbled characters during the transmission of Javascript and Asp data!

When the backend is a PHP program, maintaining the unified encoding of values ​​​​transmitted between front-end Javascript and PHP can be processed using the following function:

WEB front-end JavaScript

Encoding: encodeURI(string)

Decoding: decodeURI(string)

WEB backend Php

Encoding: urlencode(string)

Decoding: urldecode( string)

Similarly, use the corresponding encoding function encodeURI or urlencode when transferring values, and use the corresponding decoding function decodeURI or urldecode when receiving, to ensure that no garbled characters will appear during the data transfer process of Javascript and Php!

When decoding the WEB front-end Javascript and urlencode-encoded data in Php, you may need to use two decodes, such as: (decodeURI(string)) to decode some non-character text, such as punctuation marks, etc. wait.

Related learning recommendations: Programming video

The above is the detailed content of What should I do if javascript transmits Chinese garbled characters to PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template