Al-Hanashi
Al-Hanashi

Reputation: 33

Arabic in AJAX+PHP

I tried to emit Arabic text from PHP using Ajax, but it displays squares. Example from http://www.w3schools.com/ajax/ajax_aspphp.asp:

Main Page

<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script>
function showHint(str)
{
var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {

    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("GET","autoAJAX.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions:<br>  <span id="txtHint"></span></p> 

</body>
</html>

just for file (gethint.php) when using Arabic like

$a[]="المغرب";
$a[]="عمان";
$a[]="مصر";
$a[]="العراق";
$a[]="ليبيا";

when we run page it will display squares like ��


How can I solve this problem?


Example if (gethint.php) get from oracle database

    <html>

<head>

<title></title> 
</head>
<body>

<?php

//get the q parameter from URL
$q=$_REQUEST["q"];
include("connect.php");
$sqlSerchStaff = "select * from STAFF_INFO where STAFF_ID  = :ID_v"; 
$stSerchStaff = oci_parse($con, $sqlSerchStaff);
oci_bind_by_name($stSerchStaff,':ID_v', $q);    
if (!@oci_execute($stSerchStaff, OCI_DEFAULT)) {
    $e = oci_error($stSerchStaff);  // For oci_execute errors pass the statement handle
    print htmlentities($e['message']);
    print"<br> الرجاء التأكد من الرقم الوظيفي";
}
else
{

while($row = oci_fetch_array($stSerchStaff))
{  
$VID1  = $row['STAFF_NAME_AR']; 

}

$rowNo = oci_num_rows($stSerchStaff);
if ($rowNo == 0||$rowNo = null)
{
$response ="الرقم الوظيفي غير مدرج في القائمة";
//$response ="no suggestion";
//$response ="no Data";
}
else
{
//$rowNo = oci_num_rows($stSerchStaff);
$response = $VID1; 
}
echo  $response;

}

?>




</body>
</html>

and if I save gethint.php php in UTF-8 encoding it will display arabic in print"<br> الرجاء التأكد من الرقم الوظيفي"; and $response ="الرقم الوظيفي غير مدرج في القائمة";, but if it from database it will not

i try to use this //$response=iconv("UTF-8","windows-1250",$response); but it not working

I'm use oracle 10g my database NLS_CHARACTERSET is AR8MSWIN1256

and NLS_NCHAR_CHARACTERSET is AL16UTF16

Upvotes: 3

Views: 1496

Answers (1)

Michael Pryor
Michael Pryor

Reputation: 25346

You are writing in the HTML that your characters are all latin:

<content="text/html; charset=windows-1252">

I don't think you want to do that as the characters you are using are not in that charset.

try using:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

You also mention you are using an Oracle database, so you may want to check the collation on that db and that table. See the Oracle docs for more info. Essentially you need to make sure Oracle knows you are putting non latin characters into the table.

If you set the CHARSET when you create the table it will be much easier than converting the charset later.

Upvotes: 8

Related Questions