Ajax: forced utf-8 encoding
I have a webpage in UTF-8 defined in my head
<meta charset="utf-8" />
Then I have an external .js file containing the following code:
function ajax_db(){
$(document).ready(function(){
//some variables defined
$.ajax({
type:"POST",
url:"db.php",
data:"u="+uname+"&p="+pass+"&m=1",
dataType:"text",
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success: function(reply){
$("#regstat").html("<p class='status'>"+reply+"</p>");
}
});
});
}
As you can see, the request is sent to db.php, and if m is set to 1, and not all fields are filled, it ends here:
$mode = htmlspecialchars($_POST['m']);
//Mode: 1 - register, 0 - log in, 2 - log out
if ($mode == 1){
if (empty($_POST['u']) || empty($_POST['p']) || empty($_POST['email'])){
$reply = "";
echo utf8_encode($reply);
exit;
}
//more code
}
The problem is that it returns some mojibake instead of text. I tried to force utf-8 encoding, wherever I am, I entered db.php with the following lines:
mb_internal_encoding( 'UTF-8' );
header("Content-type: text/html; charset=utf-8");
No effect. In addition, if I delete the utf8_encode () function, the question mark fields are returned, which should indicate that utf-8 is not used.
Where can the problem arise?
Thank you for your time.