Accent words don’t work

Asked

Viewed 376 times

1

I have Ajax that loads database data and json type:

$.ajax({
    type:'post',
    dataType: 'json',
    url: '../model/dao/CursoDao.php?acao=1',
    success: function(dados){

        for(var i=0; dados.length > i; i++){

            $('#listaCursos').append('<a href="sala_view.php?idcurso='+ dados[i].id + '" class="btn btn-primary texto-grande botao-nivel sombra">' + dados[i].titulo +'</a>');

        }
    }
});

The function of PHP:

  public function listaCursoAjax() {

$sqlCurso = $this->conexao->prepare('SELECT * FROM curso');
$sqlCurso->execute();

while($linha = $sqlCurso->fetch(PDO::FETCH_ASSOC)) {
  $vetor[] = array_map('utf8_encode',$linha);
}

echo json_encode($vetor);

}

The problem is that the accent phrases are coming encoded: inserir a descrição da imagem aqui

  • Try to encode to Base64 and decode on the output, the accents should remain as they were :)

  • @Wesleybirth when so array_map('base64_encode',$line) get even worse

  • I’ll answer you right, you’re doing it wrong.

  • See what I edited and give me a feedback, whether it worked or not...

  • With chair will it work?

1 answer

3


Try using the following code:

    public function listaCursoAjax() {

    $sqlCurso = $this->conexao->prepare('SELECT * FROM curso');
    $sqlCurso->execute();

    while($linha = $sqlCurso->fetch(PDO::FETCH_ASSOC)) {
        foreach($linha as &$result){
            $result = utf8_encode($result);
        }
        $vetor[] = $linha;
    }


echo json_encode($vetor);
  • Continue with the blank page Wesley

  • I am without apache, so I have no way to test the code I tell you, ta difficult :/ but the logic is this face, go through the whole object $line and encode everything in it before sending.

  • I edited again, please test and tell me if it worked. If you return the blank page try replacing $vector[] = $line; with any echo to test...

  • Continues blank and gives these errors: Notice: Array to string Conversion in C: xampp htdocs vocacaosaude model dao Cursodao.php on line 77 Array Notice: Array to string Conversion in C: xampp htdocs vocacaosaude model dao Cursodao.php on line 77 Array Notice: Undefined variable: vector in C: xampp htdocs vocacaosaude model dao Cursodao.php on line 81 null

  • Remove the line $vector[] = $line; and add, in the same place, an echo "test";, post results

  • Wow, when I edited the code I kept a piece of the previous code, my mistake, test again, please.

  • It shows, but continues that in the image I posted, with a proper name, when accessing the file it is returning the good json: [{"id":"6","title":"Course - I Athlete 1"},{"id":"7","title":"Course - I Athlete 2"},{"id":"8","title":"Mesh u00c3 u00a7 u00c3 u00a3o"}]

  • So the problem is not in the creation of json, try to put this in ajax: contenttype: "application/x-www-form-urlencoded;charset=ISO-8859-15",

  • beauty I’ll try

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.