Jquery ajax call and charset windows-1252 - php

Dear stackoveflow, I have this problem. I'm working with an old version of mssql (2000) that has all the tables encoded in windows 1252 (and that's it). I can write and read succesfully with php using this line:
<?php header('Content-Type: text/html; charset=windows-1252'); ?>
If I make a normal post everything works as expected, If I do it ajax style I write messed characters in the table. I've also tried this:
contentType: "application/x-www-form-urlencoded;charset=windows-1252",
With no luck. Can anybody help me?
Thank you

I think it is possible to change the character set for incoming data from the Ajax request in Javascript somehow, butt IIRC, it's complex and is likely to have cross browser issues.
If you are querying a PHP script, the easiest way woudl be to convert the data to UTF-8 there:
$data = "Höllo, thüs üs windows-1252 encoded data";
$data_utf8 = iconv("windows-1252", "utf-8", $data);
echo $data;

Related

Reading JSON-encoded UTF-8 data from PHP service into Ruby app

My brain hurts, so I need help to solve this issue. I have read about many similar encoding problems but I can't find any info that helps me with this particular issue.
I have a PHP service which reads data from database. It sets the charset to:
mysql_set_charset('utf8', $con)
Basically it then executes a query and loops thru the db items like this:
while($row = mysql_fetch_object($result)) {
$row->MyField1 = utf8_encode($row->MyField1);
$row->MyField2 = utf8_encode($row->MyField2);
...
$res[] = $row;
}
and ends with:
print json_encode($res);
I then read the data from Ruby (a sinatra app) with:
uri = URI(str)
source = Net::HTTP.get(uri)
src = JSON.parse(source)
src.each do |s|
# Display s.MyField1 in HTML here.... HTML page is HTML5 and <meta charset="utf-8">
end
The problem is that I display strings like:
ALLMÃNHETENS ÃKNING
where 'Ã' is some unknown character to me. It should properly be 'Ä' in the first occurrence and 'Å' (Swedish A with 'ring diacritic' http://en.wikipedia.org/wiki/Ring_(diacritic)).
Is it the PHP code that is wrong? Or the Ruby code? If anyone could point me in the right direction I would be very grateful? Frankly I don't know where to start chasing bugs.
OK It seems the answer is very accurately described here:
http://www.i18nqa.com/debug/bug-utf-8-latin1.html
And the important thing is that my PHP-script must add:
Content-type: application/json; charset=utf-8
otherwise the UTF-8 will have their individual bytes interpreted as ISO-8859-1 or Windows-1252 causing exactly the described bevahiour above.
IMPORTANT ADDITION: I also had to remove the utf8_encode() call in the PHP-script since the data already was UTF-8 encoded (or maybe the mysql_set_charset("utf8", $con) did that thing, I don't know).

How to handle this PHP encoding issue?

I'm using PHP to grab some JSON from this URL - goo.gl/xdIqEy
I've tried using cURL and file_get_contents(), but the 'LastName' element always comes back looking like ─Éur─æi─ç
If I view that URL in Chrome, it looks like ÄurÄ‘ić
It should look like Đurđić
It's obviously some kind of encoding issue, but how to I handle this? The HTTP response headers don't give any clues to the encoding type.
I've tried a lot of different iconv combinations when I've got the string back in PHP - but no luck.
If I go to that URL in IE, it let's me download the .json file to disk. When I open that in Sublime Text, it looks correct.
Any advice?
You can use:
<?php
header('Content-Type: text/html; charset=utf-8');
echo iconv("utf-8", "latin1", "BosanÄić");//Bosanči
echo iconv("utf-8", "latin1", "JiráÄek"); //Jiráček
?>
Your input charset is UTF-8 you need to convert back to latin1

UTF-8 data received by php isn't decoded

I'm having some troubles with my $_POST/$_REQUEST datas, they appear to be utf8_encoded still.
I am sending conventional ajax post requests, in these conditions:
oXhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
js file saved under utf8-nobom format
meta-tags in html <header> tag setup
php files saved under utf-8-nobom format as well
encodeURIComponent is used but I tried without and it gives the same result
Ok, so everything is fine: the database is also in utf8, and receives it this way, pages show well.
But when I'm receiving the character "º" for example (through $_REQUEST or $_POST), its binary represention is 11000010 10111010, while "º" hardcoded in php (utf8...) binary representation is 10111010 only.
wtf? I just don't know whether it is a good thing or not... for instance if I use "#º#" as a delimiter of the explode php function, it won't get detected and this is actually the problem which lead me here.
Any help will be as usual greatly appreciated, thank you so much for your time.
Best rgds.
EDIT1: checking against mb_check_encoding
if (mb_check_encoding($_REQUEST[$i], 'UTF-8')) {
raise("$_REQUEST is encoded properly in utf8 at index " . $i);
} else {
raise(false);
}
The encoding got confirmed, I had the message raised up properly.
Single byte utf-8 characters do not have bit 7(the eight bit) set so 10111010 is not utf-8, your file is probably encoded in ISO-8859-1.

PHP - Encoding strikes again

I'm having encoding problems in my webpage, and it is driving me crazy. Let me try to explain
I have a meta tag defining utf8 as charset.
I'm including the scripts as utf8 too (<script type="text/javascript src="..." charset="utf8"></script>).
In .php files, I declare header('Content-Type: text/html; charset=utf8');
In my database (postgreSQL), I've made the query show lc_collate; and the return was en_US.UTF-8
I'm using AJAX
When I try to save the field value "name" as "áéíóú", I get the value "áéíóú" in the record set (using phpPgAdmin to view results).
What am I doing wrong? There's a way to fix it without using decode/encode? Someone have a good reference about theses issues?
Thank you all!
Maybe the client encoding is not set correctly? PostgreSQL automatically converts between the character encoding on the client and the encoding in the database. For this to work it needs to know what encoding the client is using. Safest is to set this when you open your connection using:
SET CLIENT_ENCODING TO 'UTF8';
For details see the docs
You might be storing the data as ISO-8859-1?
Try enconding to base64 and decoding on the other end.

how to avoid encoding problems using ajax-json and php-postrgesql

i have problems with encoding when using json and ajax.
chrome and ie encode umlauts as unicode when jsonifying, firefox and safari returning those utf-8 escaped umlauts like ¼Ã.
where is the best place to give all the same encoding?
js / php-get or by writing them to the database.
and i think the next trouble is, when i reload the utf-8 encoded stuff from the db, and write them to the browser and then rewrite them to the db, again via ajax-request a get a real chaos?
can i avoid the chaous? can i handle the encoding in an easy way?
pls. help :-)
very important is to also provide security
You must set everything to UTF-8, this means :
Database collation
Table collation
Field collation
Your coding software (example notepad++) encryption.
Had a similar problem. Maybe you are actually interpreting encoding the wrong way, clientwise. Try setting the frontend encoding before your queries.
<?php
$connection = pg_pconnect("dbname=data");
pg_set_client_encoding($connection, "encoding goes here"); //check enconding aletrnatives on PostgreSQL
$result = pg_query($connection, "SELECT whatever FROM wherever");
//and so on...
?>
I'm a newbie, but it may help. Also won't affect security in anyway if you are already protected against db injection.
Cheers

Categories