0

I'm implementing live search on my webpage. The script is fully working but results are not showing special UTF-8 characters such as "á" or "ñ". I am pretty sure that's the issue because on simple, non-live queries format is working perfectly.

I have pretty much no knowledge on JavaScript so I could use your help in order to add UTF format to this script.

//Getting value from "ajax.php".
function fill(Value) {
   //Assigning value to "search" div in "search.php" file.
   $('#search').val(Value);
   //Hiding "display" div in "search.php" file.
   $('#display').hide();
}
$(document).ready(function() {
   //On pressing a key on "Search box" in "search.php" file. This function will be called.
   $("#search").keyup(function() {
       //Assigning search box value to javascript variable named as "name".
       var name = $('#search').val();
       //Validating, if "name" is empty.
       if (name == "") {
           //Assigning empty value to "display" div in "search.php" file.
           $("#display").html("");
       }
       //If name is not empty.
       else {
           //AJAX is called.
           $.ajax({
               //AJAX type is "Post".
               type: "POST",
               //Data will be sent to "ajax.php".
               url: "../views/extension/livesearch.php",
               //Data, that will be sent to "ajax.php".
               data: {
                   //Assigning value of "name" into "search" variable.
                   search: name
               },
               //If result found, this funtion will be called.
               success: function(html) {
                   //Assigning result to "display" div in "search.php" file.
                   $("#display").html(html).show();
               }
           });
       }
   });
});
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
vic333
  • 35
  • 1
  • 6
  • 1
    Does https://stackoverflow.com/questions/553463/jquery-ajax-character-encoding help? – evolutionxbox Mar 24 '22 at 13:33
  • Not really. I've just found out that on localhost charset is working okay but when I push the site to my host charset breaks. – vic333 Mar 24 '22 at 13:54
  • 1
    If it works on localhost but not on your host, then it sounds like the issue could be the php configuration on the host. PHP should default to using UTF-8, but check that the default_charset in the php.ini file is set properly. Also check that the proper headers are set. More info on configuring PHP to use UTF-8: https://stackoverflow.com/questions/4279282/how-to-set-http-header-to-utf-8-using-php-which-is-valid-in-w3c-validator – Jscrip Mar 24 '22 at 14:33
  • 1
    Can you check the versions of the PHP being used? – qrsngky Mar 24 '22 at 14:55
  • PHP version on server was 7.4, changed to 8.1 and issue persists. Charset is breaking only on live search query which is AJAX, so I'm assuming I have some kind of error on server side not related to charset tag. – vic333 Mar 25 '22 at 07:22

0 Answers0