0

I have an HTML page that takes user input from HTML page and display it. I am calling the PHP using AJAX. so the output is display in the same HTML. There are input in the database that are in Chinese. When the data is fetched, it changes to ?????. Below is the code:

HTML:

<script>
function PostData() {
var online = navigator.onLine;
if(online){
    // 1. Create XHR instance - Start
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    // 1. Create XHR instance - End

    // 2. Define what to do when XHR feed you the response from the server - Start
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status == 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
            }
        }
    }
    // 2. Define what to do when XHR feed you the response from the server - Start

    var userid = document.getElementById("userid").value;
    var pid = document.getElementById("pid").value;
    // var image = document.getElementById("image").value;
    // 3. Specify your action, location and Send to the server - Start 


    xhr.open('POST', 'login3.php');
    //xhr.open('POST', 'config.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("userid=" + userid + "&pid=" + pid);
    //xhr.send("&pid=" + pid);
    // 3. Specify your action, location and Send to the server - End

}

else{
alert("You are offline");
}
}
</script>
</head>
<body>
<form>
    <label for="userid">User ID :</label><br/>
    <input type="text" name ="userid" id="userid"  /><br/>
    <label for="pid">Password :</label><br/>
    <input type="password" name="password" id="pid" /><br><br/>



    <div id="div1">
    <input type="button" value ="Login" onClick="PostData()" />
    </div>
    </form>
</body>

PHP:

<?php
$servername = "localhost";
  $username = "root";
  $password = "";
  $dbname = "dtable";
  mysql_query("SET NAMES 'utf8'");//this is what i tried after searching on google
  //session_start();
  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }
if(isset($_POST['userid'],$_POST['pid']))
{
    $userid = trim($_POST["userid"]);
    $pid = trim($_POST["pid"]);

    $sql = "SELECT * FROM demo WHERE username = '$userid' and password = '$pid'";
    $result = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($result);
    echo $row['week'].'<br/>'.'<br/>';

    echo '<a href="2ndHTML.html"/>'.$row['day1'].'</a>'.'<br/>';
?>
Vishu
  • 45
  • 8
  • When you try to fetch data from database using other client than PHP, eg. MySQL Workbench, what do you see? Chinese or question marks? If question marks, the problem is in the database itself (table definition, data inserts). – Jan Zahradník Jun 09 '15 at 04:26
  • 1
    You're using `mysqli_` driver not `mysql_` so `mysql_query("SET NAMES 'utf8'");//this is what i tried after searching on google` is wrong. Your code also currently is open to SQL injections. What character set is the data in your DB as? See this thread http://stackoverflow.com/questions/10331883/utf-8-php-and-mysqli-utf8 – chris85 Jun 09 '15 at 04:27
  • check **Collation** of database and table – Meenesh Jain Jun 09 '15 at 04:27
  • @MeeneshJain Collation and character set are different, http://stackoverflow.com/questions/341273/what-does-character-set-and-collation-mean-exactly. Although both should be checked. – chris85 Jun 09 '15 at 04:29
  • @chris85 The database is in Collation ut8-_general_ci – Vishu Jun 09 '15 at 04:41
  • See this thread, it should have all the information you need. At some point currently your not in UTF8. http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – chris85 Jun 09 '15 at 04:49

0 Answers0