I have a problem. After submission of a form I send request using AJAX. Everything was perfect until I tried Cyrillic text.
what I input: питання
what alerts me javascript: питання
what echos me $_POST[‘question’]: %u041F%u0438%u0442%u0430%u043D%u043D%u044F
Here’s my AJAX request:
$.ajax({
type: "POST",
url: "addQuestion.php",
data: "u_id=" + $("#u_id").val() + "&u_a_name=" + $("#u_a_name").val() + "&question="+escape($("#question_input").val()),
success: function(data) {
if (data == "Asked") {
alert("Asked");
window.location.reload();
} else {
alert(data);
}
}
});
So I thought it is AJAX problem, but I haven’t found answer in internet.
Thank you for attention.
Javascript’s
escape()doesn’t work too well with non-ASCII characters, and to handle any unicode characters I generally useencodeURIComponent()instead. In PHP, you can useurldecode()to reverse the same encoding. So:Javascript:
encodeURIComponent("питання")returns%D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8FPHP:
urldecode("%D0%BF%D0%B8%D1%82%D0%B0%D0%BD%D0%BD%D1%8F");returnsпитанняhttps://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
http://php.net/manual/en/function.urldecode.php