Problems with utf8_decode in array return

Asked

Viewed 332 times

1

I have a problem that has left me quite confused. I have a page in php that registers ministries. In it I send the data via ajax to another php page to do the insertion, and the insertion always occurs in the normal way. Follow the codes:

Ajax that sends the data

  $.ajax({
    method: "POST",
    url: "ajax/insertMinisterio.php",
    data: {
      name: name,
      alternativename: alternativename,
      description: description
    },
    success: function (result){
      var returnjson = JSON.parse(result);
      var type = returnjson['type'];
      var msg = returnjson['message'];

      alert(msg);
      //reloadMinisteriosTable();
    } // Fim success
  }) // Fim ajax

});

Page that receives data from ajax

<?php   
    include '../../class/autoload.php';

    $database = new Database();
    $ministerio = new Ministerio();

    $name = utf8_decode($_POST['name']);
    $alternativename = utf8_decode($_POST['alternativename']);
    $description = utf8_decode($_POST['description']);


    $ministerio->setNome($name);
    $ministerio->setNomeAlternativo($alternativename);
    $ministerio->setDescricao($description);
    $ministerio->setStatus(1);

    try{        
        $database->database_connect();

        $query = "INSERT INTO pibjm_ministerio (nome, nome_alternativo, descricao, status) VALUES ('{$ministerio->getNome()}', '{$ministerio->getNomeAlternativo()}', '{$ministerio->getDescricao()}', {$ministerio->getStatus()})";

        $result = mysqli_query($database->database_connect(), $query);

        if($result){
            $finalresult = array("query"=>$query, "type"=>"success", "message"=>"Ministério cadastrado com sucesso!");
        } else {
            $finalresult = array("query"=>$query, "type"=>"error", "message"=>"Não foi possível cadastrar o ministério! Tente novamente!");
        }

        echo json_encode($finalresult);
    }
    finally{
        $database->database_close_connection();
    }

?>

The big problem I’m having in using the utf8_decode. When I use it in any of the variables, it even does the normal insertion in the database, but always returns in ajax with the empty result. And I need the utf8_decode because it leaves the accent right. If I use htmlentities, utf8_encode, or any other php function in the variables, everything also works, however in ajax return back the data that I desire, But in the bank register the accentuation is wrong. I tested several solutions and I can not result. What problem utf8_decode may be giving there in php?

1 answer

1


The json_encode function only accepts values in utf8, if it has a single character in another encoding it will return null.

Apply utf8_encode to all array elements before passing it to json_encode.

  • hum beauty Jader, then I’ll test here and I’ll tell you

  • While further analyzing your report, I realized that in fact you are having trouble recovering the data from the bd and are probably trying to get it by ajax/json, so I edited the answer.

  • So Jader, solved my problem, but I did not apply utf8_encode on all elements of the array, I only passed the query and solved!

Browser other questions tagged

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