0plus1 Blog

Tag: utf-8

Ajax calls vs MSSql

by admin on Jun.29, 2010, under Programming

Please note that this article can be applied to any non utf-8 encoding.

I recently needed to send data using ajax to an MSSql 2000 database (which requires windows-1252).
As you may know javascript sends data encoded in utf-8 (utf-16 to be more precise), this must be accepted as a fact thus making the use of a php function to convert the encoding before sending to the database. Here is the snippet I use:

function convertor($array){
	if (is_array($array) && count($array) > 0){
		foreach ($array as $k => $v){
			$return[$k] = mb_convert_encoding($v, "iso-8859-1","UTF-8");
		}
	} elseif (!empty($array)) {
		$return = mb_convert_encoding($array, "iso-8859-1","UTF-8");
	} else {
		return false;
	}
	return $return;
}

Basically what it does is to check if we have an array or a single value then proceed to convert it to the desired encoding (in my case iso-8859-1), after converting the values these can be safely stored into the database.

I have also to add that I’ve encountered some jquery plugins that messes with the encoding of the strings. To be on the safe side you may use this line:

unescape(encodeURIComponent(string))
Leave a Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Archives

All entries, chronologically...