Jump to content

Recommended Posts

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

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

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-8)

 

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?

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.