CancelImage Upload

How to send Russian Cyrillic and other symbols via a form using AJAX

Encoding data to be sent via AJAX can turn into a nightmare in some languages. Usually encoding the data with the JavaScript function encodeURIComponent() and decoding it with a function such as PHP's rawurldecode() will prove enough to send and receive data the other end via AJAX. However, this is not always the case. Recently I had a Russian client who needed a guestbook. And although the AJAX worked fine in sending Latin characters, and even managed to post German and French letters such as ä, ë, ö and ö, the Russian characters were misinterpreted.

Now I am sure there is a more convenient way to get around this problem, but I managed to find an acceptable workaround that served my purpose of allowing comments to be written in multiple character sets. The solution was to convert the whole data string that was to be sent into ASCII code before it was sent, and then to revert it back into symbols the other end using PHP's html_entity_decode().

On the JavaScript side of things the encoding ended up being along these lines:

function fetchAscii(obj)
{

      var convertedObj = '';

      for(i = 0; i < obj.length; i++)
      {

            var asciiChar = obj.charCodeAt(i);

            convertedObj += '&#' + asciiChar + ';';

      }

      return convertedObj;

}


function sendData(obj1)
{

      obj1 = fetchAscii(obj1);

      var poststr = "obj1=" + encodeURIComponent( obj1 );

      ...

}

And on the PHP side of things we can then decode the variable:

$obj1 = html_entity_decode($_POST['obj1']);


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com