gmc1103 Posted June 5, 2015 Share Posted June 5, 2015 Hi I have defined my database as utf-8 but when i insert i get strange character inserted This is my ajax form <script type="text/javascript"> $(document).ready(function () { $("#formacao").submit(function (e) { e.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: 'nova_formacaoBD.php', type: 'POST', data: formData, cache: false, contentType: false, processData: false, beforeSend: function () { $('#ajaxDivAlert').show().html('A carregar o ficheiro...aguarde por favor'); } }).done(function (data) { var result = data.status; if (result == 'success'){ $('#ajaxDivAlert').fadeOut(); $('#ajaxDivOk').show().html('Informação: ' + data.message); $("#ajaxDivOk").fadeOut(5000); } }).fail(function (data) { var result = data.status; if (result == 'error'){ $("#ajaxDivErro").html('Informação: ' + data.message); $("#ajaxDivErro").fadeIn(); $("#ajaxDivErro").fadeOut(5000); } }); }); }); And this is insert php <?php header('Content-Type: application/json'); error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); $mysqli = new mysqli('localhost', 'xxxxx', 'xxxxx'); if (mysqli_connect_errno()) { trigger_error('Database connection failed: ' . mysqli_connect_error(), E_USER_ERROR); } $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/uploads'; $uploadfile = $uploaddir . '/' . basename($_FILES['fileToUpload']['name']); $escola = filter_input(INPUT_POST, 'escola'); $form = filter_input(INPUT_POST, 'form'); $data =filter_input(INPUT_POST, 'data'); $horas = filter_input(INPUT_POST, 'horas'); $local = filter_input(INPUT_POST, 'local'); $dest = filter_input(INPUT_POST, 'dest'); $datas = filter_input(INPUT_POST, 'datas'); $visto = 0; echo $form; if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) { $target_path = "/uploads/" . basename($_FILES['fileToUpload']['name']); $sql = "INSERT INTO `ebspma_paad_ebspma`.`formacoes`(idescola, nome, inicio, horas, local, destinatarios, dataLimite, visto, path) VALUES(?, ?, ?, ?, ? ,?, ?, ?, ? )"; $stmt = $mysqli->prepare($sql); if ($stmt === false) { trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $mysqli->error, E_USER_ERROR); } $stmt->bind_param('issssssis', $escola, $form, $data, $horas, $local, $dest, $datas, $visto, $target_path); if (!$stmt->execute()) { echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada')); } } else { echo json_encode(array('status' => 'error', 'message' => 'Opppss...A formação não foi gravada')); } $stmt->close(); echo json_encode(array('status' => 'success', 'message' => 'Nova formação gravada')); The word inserted is Informação instead of Informação And the strange is i don't get in my <div> the result but in debugger i see Informação{"status":"success","message":"Nova formação gravada"} Whats wrong? Link to comment https://forums.phpfreaks.com/topic/296667-strange-character-when-insert/ Share on other sites More sharing options...
cyberRobot Posted June 5, 2015 Share Posted June 5, 2015 Is your HTML page configured to be UTF-8? How about PHP? How about your MySQL connection? For what it's worth, the following article helped me make the switch to UTF-8: http://htmlpurifier.org/docs/enduser-utf8.html Link to comment https://forums.phpfreaks.com/topic/296667-strange-character-when-insert/#findComment-1513288 Share on other sites More sharing options...
gmc1103 Posted June 5, 2015 Author Share Posted June 5, 2015 My form has utf-8 And the database has utf-8 generic So i don't get whats wrong Link to comment https://forums.phpfreaks.com/topic/296667-strange-character-when-insert/#findComment-1513298 Share on other sites More sharing options...
cyberRobot Posted June 5, 2015 Share Posted June 5, 2015 Does your HTML page have a <meta> tag where the charset attribute is set to "utf-8"? More information can be found here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta If so, does your browser recognize the page encoding. For more information on how to check, see the following: http://htmlpurifier.org/docs/enduser-utf8.html#findcharset Is PHP set to use UTF-8? Note that you can use ini_set() and the "default_charset" configuration option to change the encoding a runtime. More information here: http://php.net/manual/en/function.ini-set.php Is the MySQL connection object set to use UTF-8? Note that you can use mysqli_set_charset() to change the encoding. More information can be found here: http://php.net/manual/en/mysqli.set-charset.php Link to comment https://forums.phpfreaks.com/topic/296667-strange-character-when-insert/#findComment-1513303 Share on other sites More sharing options...
gmc1103 Posted June 6, 2015 Author Share Posted June 6, 2015 Hi Does your HTML page have a <meta> tag where the charset attribute is set to "utf-8"? Yes, Encoding : UTF-8 If so, does your browser recognize the page encoding? Chrome (Automatic detection (UTF- Is PHP set to use UTF-8? <?php $mysqli = new mysqli('localhost', 'xxxxx', 'xxxxxx'); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } /* change character set to utf8 */ if (!$mysqli->set_charset("utf8")) { printf("Error loading character set utf8: %s\n", $mysqli->error); } else { printf("Current character set: %s\n", $mysqli->character_set_name()); } $mysqli->close(); ?> Response : Current character set: utf8 Is the MySQL connection object set to use UTF-8? I put this and now it works mysqli_set_charset($mysqli,"utf8"); The main problem now is i don't get the json_encode response into my <div> but in debugger is received Informação{"status":"success","message":"Nova formação gravada"} What's wrong here? <script type="text/javascript"> $(document).ready(function () { $("#formacao").submit(function (e) { e.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: 'nova_formacaoBD.php', type: 'POST', data: formData, cache: false, contentType: false, processData: false, beforeSend: function () { $('#ajaxDivAlert').show().html('A carregar o ficheiro...aguarde por favor'); } }).done(function (data) { var result = data.status; if (result === 'success'){ $('#ajaxDivAlert').fadeOut(); $('#ajaxDivOk').show().html('Informação: ' + data.message); $("#ajaxDivOk").fadeOut(5000); } }).fail(function (data) { var result = data.status; if (result === 'error'){ $("#ajaxDivErro").html('Informação: ' + data.message); $("#ajaxDivErro").fadeIn(); $("#ajaxDivErro").fadeOut(5000); } }); }); }); </script> Any error? Link to comment https://forums.phpfreaks.com/topic/296667-strange-character-when-insert/#findComment-1513331 Share on other sites More sharing options...
cyberRobot Posted June 8, 2015 Share Posted June 8, 2015 I haven't used jQuery with Ajax before, so this is just a guess. Have you tried commenting out the if test? }).done(function (data) { var result = data.status; //if (result === 'success'){ $('#ajaxDivAlert').fadeOut(); $('#ajaxDivOk').show().html('Informação: ' + data.message); $("#ajaxDivOk").fadeOut(5000); //} If the text appears in your <div> tag, you'll need to look closer at what data.status is returning. Link to comment https://forums.phpfreaks.com/topic/296667-strange-character-when-insert/#findComment-1513455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.