I will ask again something similarly with risk of my question begin closed(i cannot delete the old thread,because it might get me banned,it says)..So i need to decode an ajax string which i am passing from HTML form to ajax and on to PHP.When i write in English and check values which is passed it is all right:
But when i write some in cyrillic this 'thing' is sended to php (word is "ку",same as "ku" in english)
I am trying to fix this problem from like 24 hours and read SO much information that i got overwhelmed and totally blocked. So once again there is my code:
jQuery(document).ready(function ($) {
new InputStreamReader(conn.getInputStream(), "UTF-8"));
$("#food_search").keyup(function(event){
var search_term =$(this).val();
$.ajax({
type:"POST",
url:"http://test.com/bg/%D1%82%D1%8A%D1%80%D1%81%D0%B5%D0%BD%D0%B5-%D0%BD%D0%B0-%D1%85%D1%80%D0%B0%D0%BD%D0%B8/",
data:{'fsearch':search_term},
success:function(res){
$("#food_search_result").html(res);
console.log(res);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(xhr.responseText);
alert(thrownError);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!----------------------------------------------------------------
HTML
----------------------------------------------------------------->
<form method="post" accept-charset="UTF-8">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<div id="food_search_result"></div>
<!----------------------------------------------------------------
PHP
----------------------------------------------------------------->
<?php
$hostname = "localhost";
$username = "gosho";
$password = "0!ijgls9df";
$databaseName = "dbName";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$fsearch="";
if(!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];
$req = $connect->prepare("SELECT title FROM food_data_bg WHERE title LIKE ?");
$value = '%'.$fsearch.'%';
$req->bind_param('s', $value);
$req->execute();
$req->store_result();
$num_of_rows = $req->num_rows;
$req->bind_result($title);
if ($req->num_rows == 0){
echo 'Няма резултати';
}
else{
while($data=$req->fetch()){
?>
<div class="search-result">
<span class="result-title"><?php echo $title; ?></span>
</div>
<?php
}
var_dump($_POST['fsearch']);
$req->free_result();
}
}
?>
So for short this is Search engine which must check on every key up if there is a match in database with the inputed text and display it if there is. And my question is:
How to decode the ajax information sended to php to display cyrillic characters as usual and not like %D0%BA and so on..
If you guys need additional info,to help me feel free to ask.Thank you all <3
</div>
Guess the problem wasn't totally clear for me,but i fixed the problem the day after i asked the question.Andrew's answer is really awesome for sumbit button,but for now i won't make any button SOOO i fixed the problem by adding a single line of right AFTER the $connect and the code is:
$connect->set_charset("utf8");
The problem wasn't with any deconing,encoding atleast for now i saw that when i type like "" or / php returns \"\" or soo think for future i will need an of your advices for decoding,encoding so thank you guys ! <3 Once again i solved the problem with your help
If u wand send some data to server first of all u need serialize
your data.
var request;
$("#foo").submit(function(event) {
// Prevent default posting of form - put here to work in case of errors
event.preventDefault();
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input");
// Serialize the data in the form
var serializedData = $form.serialize();
// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);
// Fire off the request to /form.php
request = $.ajax({
url: "/form.php",
type: "post",
data: serializedData
});
// Callback handler that will be called on success
request.done(function(response, textStatus, jqXHR) {
// Log a message to the console
console.log("Hooray, it worked!");
});
// Callback handler that will be called on failure
request.fail(function(jqXHR, textStatus, errorThrown) {
// Log the error to the console
console.error(
"The following error occurred: " +
textStatus, errorThrown
);
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function() {
// Reenable the inputs
$inputs.prop("disabled", false);
});
});
<form method="post" accept-charset="UTF-8" id="foo">
<p>Търсене на храни: <input type="text" name="fsearch" id="food_search"></p>
</form>
<div id="food_search_result"></div>
PHP (that is, form.php):
$fsearch = isset($_POST['fsearch']) ? $_POST['fsearch'] : null;
And try add this code to html. documentation about this
<head>
<meta charset="UTF-8">
</head>
</div>