Solve the problem of submitting Chinese characters garbled by Get method in ThinkPHP

 

I have only been in contact with thinkPHP for a few days, and I haven’t even started. Because the company’s projects are very anxious, I am also very interested in thinkPHP. I want to take the opportunity to improve, so I anxiously started with thinkPHP. Of course, I encountered a lot of problems, but Fortunately, through the help of my friend gidot , the network and the official manual, it was basically solved.

In the past few days, I have encountered another problem, that is, the problem of garbled characters in Chinese when GET is submitted. Looking at the forum, it seems that many people have it and there is no good solution.
Database, page encoding, and file encoding are all UTF8, which is 100% sure there is no problem.
Some people provide a feasible method, but it does not work for me. It may be because my environment is WIDNOWS2003+IIS6.0 and the default character set is Chinese. Tried various methods to no avail.

Later, I saw that the client base64 encoded and then sent the request, and then BASE64 decoded it on the server. I tried it and found that the base64 result of PHP is actually different from that of JS -_-|||. Later, I thought that the problem was in the encoding during transmission. Using base64 only converts characters into letters to avoid being encoded and decoded again. problem appear.
In this case, writing a simple codec by yourself should also be able to solve it. I tried it, and it was solved. Now I will share my method.

First, encode the data sent to the client, so as not to be encoded by the browser to the server during transmission, my solution is to use JS to encode the character encodeURI when sending the form, and then convert the percentage of Replace the "%" with other characters that will not be encoded (note that this character should be special, not to be confused with the characters entered by the user, and it should not be too long. Considering that one character is easy to be confused with the user's input, I chose 3 characters character ".s.",), and then let the form send. Implementation:

code
<form method='get' id="search1" action="__URL__">
<input value="{$Think.get.group_name|getChineseGetValue}" type="text" name="group_name"id="group_name" title="组名查询" class="medium" >
<html:imageBtn type="button"click="getId('group_name').value=encodeURI(getId('group_name').value).replace(/%/g,'.s.');
getId('search1').submit();"class="search imgButton" value="查询" style="impBtn hMargin fLeft shadow" />
</form>


Then write a function on the server side to decode our encoded data into normal:
code

//解码GET传值
function getChineseGetValue($key){
if (strlen($key)>0){
  return urldecode(str_replace('.s.','%',$key));
}
}



Then we need to decode when we receive the parameter query:
code
$map['group_name'] = array('like',"%".getChineseGetValue($_GET['group_name'])."%");


Finally, when the GET value is displayed on the page, it is also decoded:
code

<form method='get' id="search1" action="__URL__">
<input value="{$Think.get.group_name|getChineseGetValue}" type="text" name="group_name" id="group_name" title="组名查询" class="medium" >
<html:imageBtn type="button" click="getId('group_name').value=encodeURI(getId('group_name').value).replace(/%/g,'.s.');getId('search1').submit();" class="search imgButton" value="查询" style="impBtn hMargin fLeft shadow" />
</form>



That's it, I hope this post can be helpful to children's shoes who have this problem. By the way
, that getId is a JS function of mine, which is used lazily (you can write document.getElementById without so much trouble...), code show as below:
code
function getId(id){
return document.getElementById(id);
}

 

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324131296&siteId=291194637