2

I am using DataTabels with PHP, AJAX, jQuery.

I get some data from MySQL database and when it contains some specific characters, then it crashes (characters like ä ü õ etc).

Here is where I "fetch data" to my table:

function fetch_data()
{
   var dataTable = $('#user_data').DataTable({
     "processing" : true,
     "serverSide" : true,
     "order" : [],
     "ajax" : {
       url:"fetch.php",
       type:"POST"
     }
   });
}

and this is my fetch.php where I make the output :

$output = array(
 "draw"    => intval($_POST["draw"]),
 "recordsTotal"  =>  get_all_data($connect),
 "recordsFiltered" => $number_filter_row,
 "data"    => $data
);

echo json_encode($output);

I tried to add

contentType: "application/json; charset=utf-8",
dataType: "json",

To fetch_data function, but then my result just went empty... Where am I wrong?

Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38

1 Answers1

2

I guess you have MySQL query in get_all_data function, I suggest add mysqli_set_charset($connection, "utf8"); to your mysql connection .some thing like the following will resolved your poroblem

$connection = new mysqli($server, $user, $pass, $database);
mysqli_set_charset($connection, "utf8");

also you can try json_encode( $output, JSON_UNESCAPED_UNICODE ); instead of json_encode($output);

Yuseferi
  • 7,931
  • 11
  • 67
  • 103