Jump to content

Ajax charset encoding


maverick5x

Recommended Posts

Hello,

 

I am having problems developing an arabic website using PHP/MySQL and ajax. Arabic text in pages is displayed as is with no problems. but when data is posted from an html form the data are saved in the database in UTF-8 encoding. I though it was from the mysql collation thing and charsets but it wasn't. I discovered that when data is sent to the PHP script, it reads them as utf-8 strings although i did specify the charset in the ajax post request

xmlHttp.onreadystatechange=AjaxStateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=Windows-1256");
xmlHttp.send(postdata);

 

I knew that by replacing html that should be displayed as a response to my page by the sql statement that contains the values posted and they appear as some wierd characters. So we can tell it's not the mysql, nor ajax(it shows correct encoding in "alert" before posting).

 

I think i can say it's in the http connection. Is there a solution to this one? i cannot convert to utf-8 because i have a huge database with arabic (windows-1256) data on it.

 

Hope someone has the answer

 

best regards,

Rakan

 

Link to comment
Share on other sites

  • 4 weeks later...

try following

 

 

for utf8
header("Content-Type: text/html; charset=utf8\n");
for Windows-1256
header("Content-Type: text/html; charset=Windows-1256\n");

 

with the help of above it will set charset to Windows-1256 for current page data display.

 

use in the top of the page

 

<?php 

header("Content-Type: text/html; charset=Windows-1256\n");

?>

Link to comment
Share on other sites

If your javascript is in an external file, you can also try setting a charset for that file (I have had similar problems working with Japanese):

 

<script type="text/javascript" src="javascript/user_details.js" charset="EUC-JP"></script>

 

note: replace 'EUC-JP' with the charset of your choice. That just happens to be the one I use.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.