Wednesday, February 17, 2010

Ajax charset issue in CakePHP

I was facing a typical issue in the internationalisation with cakephp.

My requirement was to render french characters in the normal flow.

My form fields were accepting the french characters in the normal flow. They were stored and retrieved normally.

But, in the Ajax flow, for eg in comments section, the french characters didnt reach the backend in the normal charset. I was breakinig my head very badly.

I found the solution in one of the forums. The problem was with the prototype.js file, where the character set was set as default to UTF-8. But the french characters need ISO-8859-1.

There are two solutions.

First Solution :

I could have updated to the latest version of the prototype.js and then try to send the encoding as a option in the ajax form field. That would have solved the problem. But I couldnt update to the latest file since I had so many stuff dependent on the existing version. I dont want to take any risk.

Second Solution:

The other option was really cool one.

$name = iconv("UTF-8", "windows-1256", $_POST['name']);

That is PHP gives you a method to convert the charset from any given charset to any required charset.

I simply used the iconv to fix the issue.


No comments:

Post a Comment