How can i decode special characters in datatable?

How can i decode special characters in datatable?

prede8prede8 Posts: 3Questions: 1Answers: 0
edited March 2020 in Free community support

Hello,

i have a datable that works well, but i have a proble with special characters like è,à or ù.

When i try to display one of this i see a very strange char.

Someone can help me?

This is my code

$('#clientTableId').DataTable( {
    "order": [[ 0, "asc" ]],
    ajax : {
        url :url,
        type: 'POST',
        dataSrc:function ( json ) {

            return json;
        },

    },
    "language": {
        "lengthMenu": lengthMenuT,
       // "zeroRecords": zeroRecordsT,
        "info": infoT,
        "infoEmpty": infoEmptyT,
        "infoFiltered": infoFilteredT,
        "search": searchT,
        "paginate": {
            "previous": previousT,
            "next": nextT
        }
    },


    columns: [

        { "data": "Cognome",
            render: function (data,type,row) {

        
                return " <div onclick='goToClientPage("+row.id.clients.patientId+")' style='width: 100%; height: 80px'><label style='text-decoration: underline' class='cliente-"+row.id.clients.active+"'>"+row.id.clients.surname+"</label></div>"

            }},
        { "data": "Nome",
            render: function (data,type,row) {
           
                return " <div onclick='goToClientPage("+row.id.clients.patientId+")' style='width: 100%; height: 80px'><label style='text-decoration: underline'>"+row.id.clients.name+"</label></div>"
            }
        },
....

and this is the resuts

jhon mari�������������

i have already verified that in my java code the returned string has the correct chars è, ò, à.

Thanks to everyone for helping me

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    edited March 2020

    Let's assume you get your data from an SQL database. Then you would need to set the character set of the database to UTF-8 like in here. I remember using the db handler for the first time and it didn't have "charset" specified in "$dsn" because in US English it isn't needed. I had the same problems with special characters.

    define("DB_CHARSET",        "utf8mb4");
    ....
    
    class Database
    {
        private $host = DB_HOST;
        private $dbname = DB_NAME;
        private $charset = DB_CHARSET;
        private $user = DB_USER;
        private $pass = DB_PASS;    
        
        private $dbh;
        private $error;
        
        private $stmt;
        
            
        public function __construct() {
             // Set DSN
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . 
                   ';charset=' . $this->charset;
            // Set options
            $options = array(
                PDO::ATTR_PERSISTENT    => true,
                PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
            );
            // Create a new PDO instanace
            try{
                $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
            }
                catch(PDOException $e) {  
                    $this->error = $e->getMessage();               
                    file_put_contents('PDOErrors.txt', $this->error, FILE_APPEND);
                    die ("Database Connection Error");
            }
        }    
    ...
    
  • prede8prede8 Posts: 3Questions: 1Answers: 0
    edited March 2020

    Hi, thanks for your reply. Unfortunately this is not the solution, my database is encoding in UTF8 and as i said when i retriving data from my batabase on my java code the characters are well formed. they change when arrive via Json to my table

  • kthorngrenkthorngren Posts: 21,169Questions: 26Answers: 4,922

    Can you post an example of the JSON string by copying it from the browser's network inspector? This will show us what Datatables is receiving.

    Kevin

  • prede8prede8 Posts: 3Questions: 1Answers: 0
    edited March 2020

    Hi Kevin,

    This is what i send from java controller via ajax:

    "clients":{
    "patientId":132906,
    "password":"4A29E06E",
    "active":1,
    "name":"mariààààèèèòòòùùù",
    "surname":"giovanni",
    "alias":"gabriele P.",
    ....
    

    and this is what i see in browser inspection,

    clients:
    patientId: 132906
    password: "4A29E06E"
    active: 1
    name: "mari�������������"
    surname: "giovanni"
    alias: "gabriele P."
    ....
    

    I don't know where the transformation occurs

  • rf1234rf1234 Posts: 2,946Questions: 87Answers: 416
    Answer ✓

    I think you are actually sending the broken values because they are like that in the browser's network inspector - and that is what data tables is receiving.

    The problem is: The visual representation of what you are sending and that you look at doesn't show you what you are actually sending. I had the same problem - and it was really mind boggling.

    For some interface I had to send an awfully looking header to the client. It looked fine in my debugger using PHP. But when I opened the result with Excel as a csv file all the special characters were broken like in your example.

    The solution was: convert it all to "Windows-1252" which is the format Excel likes. My default is UTF-8 but that wasn't good enough in this case. All you can do is experiment a little I guess ...

    return
    mb_convert_encoding(  
    'Umsatz;S/H Kz.;Währ.;;;;Konto;Gegenkto;;Belegdatum;Bel-Feld1;'
    . 'Bel-Feld2;;Buchungstext;;;;;;;;;;;;;;;;;;;;;;;Kost1;Kost2;;;;;;;;;;'
    . 'Zusatzinfo-Art 1;Zusatzinfo-Inhalt 1;Zusatzinfo-Art 2;Zusatzinfo-Inhalt 2;'
    . ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Zahlweise;Forderungsart;;'
    . 'Zug.Fälligkeit;;;;;;;;;;;;;;;;;;;;;Festschreibung;;Datum Zuord.;'
            ,
    "Windows-1252" );
    
This discussion has been closed.