When I developed a PHP web application with AJAX. I encountered a problem that JavaScript cannot process utf-8 character correctly. Whenever user enter a UTF-8 string,
it was converted to ASCII in JavaScript and delivered to PHP script. The site is using UTF-8 characters and will be sure there are a lot of request using utf-8 characters. I googled “Javascript and utf-8” and find an article at http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html. It provides two functions for the purpose. One function I will use is the decode_utf8. I simply copied these two functions and the second one did not work. I change the inner function from escape to unescape. After the change, I call the function decode_utf8(s).

Here are the two functions:
function encode_utf8( s )
{
return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
return decodeURIComponent( unescape( s ) );
}

A JavaScript code snippet to call the function:

function search(php)
{
var keyword = window.document.forms["searchform"].elements["keyword"].value;
var url=php+"?cmd=search" +
"&keyword="+decode_utf8(keyword);
makeGETrequest2(url, link_callback);
}

Share

Tags: , , , , ,