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.
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>
Related learning recommendations: javascript video tutorial
PHP code:
<?php $keyword = (isset($_GET['keyword'])) ? $_GET['keyword'] : ''; ?>
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!