By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,154 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,154 IT Pros & Developers. It's quick & easy.

Unable to post arabic text in Ajax using php

100+
P: 115
hi i'm trying to insert an Arabic word into the database in Ajax using the POST method .
for example i want to insert "ت" but i'm getting "%u062A" instead ... does any one can help me ?
thanks in advance
Nov 2 '11 #1
Share this Question
Share on Google+
18 Replies


100+
P: 1,059
What encoding you are using??
Nov 3 '11 #2

100+
P: 115
Hi i'm using UTF-8 and thanks for your help :)
Nov 3 '11 #3

zorgi
Expert 100+
P: 431
HM... It does sound like encoding issue. Is everything UTF-8 from HTML and file encoding to database tables and fields? Did you try inserting values directly into database?
Nov 3 '11 #4

100+
P: 115
Yes i'm inserting the values directly to database , and i have 2 fields in just one table , a string and a text fields, and both of them are in utf8_general_ci in the database.
but i couldn't manage to get the HTML UTF-8 , cause i always get for example: "%u062A" instead of "ت" in the database.
how could i solve this i read a lots of articles about this and i still i couldn't fix it.
And @Zorgi thanks for your reply
Nov 4 '11 #5

100+
P: 1,059
I just have made a test for you.

I found that if the encoding is other than UTF-8 then you will get % preceding characters.

But if you use UFT-8 then it will act properly.

add this line at the head section of your html page
Expand|Select|Wrap|Line Numbers
  1. <META http-equiv=Content-Type content="text/html; charset=utf-8"> 
  2.  
I believe it will work
Nov 4 '11 #6

100+
P: 115
this is my code, and hope someone help me !!
this is the jquery:
Expand|Select|Wrap|Line Numbers
  1. connection.overrideMimeType('text/plain', charset="utf-8" );
  2. function trim(str){
  3. return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');}
  4. function totalEncode(str){
  5. var s=escape(trim(str));
  6. s=s.replace(/\+/g,"+");
  7. s=s.replace(/@/g,"@");
  8. s=s.replace(/\//g,"/");
  9. s=s.replace(/\*/g,"*");
  10. return(s);
  11. }
  12.  
  13. function connect(url,params)
  14. {
  15. var connection;  // The variable that makes Ajax possible!
  16. try{// Opera 8.0+, Firefox, Safari
  17. connection = new XMLHttpRequest();
  18. }
  19.  
  20. catch (e){// Internet Explorer Browsers
  21. try{
  22. connection = new ActiveXObject("Msxml2.XMLHTTP");
  23.  
  24. }
  25. catch (e){
  26. try{
  27. connection = new ActiveXObject("Microsoft.XMLHTTP");}
  28. catch (e){// Something went wrong
  29. return false;}}}
  30. connection.open("POST", url, true);
  31. connection.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  32. connection.setRequestHeader("Content-length", params.length);
  33. connection.setRequestHeader("connection", "close");
  34. connection.send(params);
  35. return(connection);
  36. }
  37. function validateForm(frm){
  38. var errors='';
  39. var str=trim(document.frm.name.value);
  40. if (str.length > 20){
  41. errors+='post name must be within 20 characters.\n'; }
  42. var str=trim(document.frm.comment.value);
  43. if (str.length > 100){
  44. errors+='post name must be within 100 characters.\n'; }
  45.  
  46. if (errors){
  47. alert('The following error(s) occurred:\n'+errors);
  48. return false; }
  49. return true;
  50. }
  51. function submitForm(frm){
  52. if(validateForm(frm)){
  53.  
  54. document.getElementById('feedback').innerHTML='';
  55. var url = "formdata.php";
  56. var params = "name=" + totalEncode(document.frm.name.value ) + "&comment="+totalEncode(document.frm.comment.value );
  57. var connection=connect(url,params);
  58.  
  59.  
  60. connection.onreadystatechange = function(){
  61. if(connection.readyState == 4){document.getElementById('feedback').innerHTML=connection.responseText;}
  62. if((connection.readyState == 2)||(connection.readyState == 3)){document.getElementById('feedback').innerHTML = '<span style="color:green;">Sending request....</span>';}}}
  63. }
  64.  
and this is the formdata.php
Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. </head>
  4. <?php
  5. $name=trim(addslashes(htmlspecialchars(rawurldecode($_POST["name"]))));
  6. $comment=trim(addslashes(htmlspecialchars(rawurldecode($_POST["comment"]))));
  7. if(empty($name)||empty($comment)){
  8. echo '<font color="red">Please fill the form</font>';
  9. exit;}
  10. $comment=str_replace("\n","<br>",$comment);
  11. $data='Data submitted by '.$name.'<br><big>Comment:</big><br>'.$comment;
  12. //echo $comment;
  13. include_once "connect.php";
  14. $y=iconv(mb_detect_encoding($comment), 'UTF-8', $comment);
  15. $query="INSERT INTO `ajaxtest`.`information` (`info_id` ,`info` ,`comment`) VALUES (NULL , '".$name."','".$comment."')";
  16.  
  17. echo $query;
  18. @mysql_query($query);
  19. ?>
  20.  
Like you can see above I'm trying to post a word to the database. that word could be in English or in Arabic . the English side is working but the Arabic is not working .
For example i want to insert "ت" but i'm getting "%u062A" instead in the database !!

1- I'm saving the data in the database as "utf8_general_ci"
2- I used this code that i say it in lots of answers:
Expand|Select|Wrap|Line Numbers
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2.  
But that did not resolve the problem. I posted the code cause i read a lots of articles and i'm going crazy ! hope that someone will help me ! thanks in advance ! :)
Nov 4 '11 #7

100+
P: 1,059
try
Expand|Select|Wrap|Line Numbers
  1. onnection.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
instead
Expand|Select|Wrap|Line Numbers
  1. onnection.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Nov 4 '11 #8

100+
P: 115
@johny10151981 ... I added the code that I'm using, and i added the code that you gave me, still it's not working .
Thanks for your reply
Nov 4 '11 #9

100+
P: 1,059
I just checked it on IE6, it does not work, now I am confused, are you using IE
Nov 4 '11 #10

100+
P: 115
No i'm using Firefox
Nov 4 '11 #11

zorgi
Expert 100+
P: 431
Putting your meta tags in correctly is not enough. Your file has to be UTF-8 encoded too. Open it using notepad and if Encoding selected is not UTF-8 change it to UTF-8 and save as UTF-8.
Nov 4 '11 #12

100+
P: 115
@Zorgi,
I opened the files in notepad and i got the same code .
How can i know if the UTF-8 changed?
Thanks for your reply
Nov 4 '11 #13

zorgi
Expert 100+
P: 431
Just try to save the file: file -> save as and and Encoding should be last option
Nov 4 '11 #14

100+
P: 115
Yes i have :
1- C (Canonical Decomposition, followed by Canonical Composition)
2- D (Canonical Decomposition)
3- KC (Compatibility Decomposition, followed by Canonical Composition)
4- KD (Compatibility Decomposition)
wich one ??
i'm confused
Nov 4 '11 #15

zorgi
Expert 100+
P: 431
notepad offers you those options?
Nov 4 '11 #16

100+
P: 115
@Zorgi, I change it to UTF-8 from notepad ,
i did save as and i chose UTF-8 . But the problem still the same
Nov 8 '11 #17

P: 9
WOW! so many people reply??!
Nov 10 '11 #18

100+
P: 115
does anyone knows how to convert from:
'u0104' : the unicode equivalent notation
to:
Expand|Select|Wrap|Line Numbers
  1. '& #260;'
represents the character code in decimal ??
That will resolve my problem thanks a lot !
Nov 14 '11 #19

Post your reply

Sign in to post your reply or Sign up for a free account.