I have a php file that uses jQuery.ajax() to capture some data from another php file in a div.
jQuery.ajax({ type: 'POST', encoding:"UTF-8", dataType:"html", contentType: "text/plain; charset=UTF-8", url: '/path/data.php', success: function(msg) { jQuery('#dataBox').html(msg); } });
My problem is that if I have some "special" characters in the data that I receive through ajax, for example, Γ₯Àâ, then I get a "question mark in black diamond". If I open an external file in a browser, it will work. If I put some special characters on the main page, this works.
Some simplified codes:
data.php:
$mysqli = new mysqli("localhost", "username", "pass", "db"); $mysqli->set_charset("utf8"); $mysqli->query("SET GLOBAL time_zone = '+00:00'"); $stmt = $mysqli -> prepare("SELECT GROUP_CONCAT(sometext) AS mytext FROM `mytable`"); $stmt -> execute(); $results = selectResults($stmt); $stmt -> close(); $mysqli -> close();
selectResults:
function selectResults($stmt) { $parameters = array(); $results = array(); $meta = $stmt->result_metadata(); while ( $field = $meta->fetch_field() ) { $parameters[] = &$row[$field->name]; } call_user_func_array(array($stmt, 'bind_result'), $parameters); while ( $stmt->fetch() ) { $x = array(); foreach( $row as $key => $val ) { $x[$key] = $val; } $results[] = $x; } return $results; }
data.php:
foreach($results as $result){ $textArray = explode(',', $result['mytext']); } foreach($textArray as $text){ echo($text); }
source share