0

I try since few hours to understand why this happend, I explain myself :

I send an ajax request to my api with an FormData, every fields are good at this moment. When he was send to my API some field (like $_POST["Description"]) looks different..

My JavaScript:

    document.getElementById("sendAddQuestion").onclick = function () { 
        if (document.getElementById("inputIntitule").value!="" && document.getElementById("inputDescription").value!="" && $("#tagsInputList").val()!="") {

            var lsttags = "";
            var str = $("#tagsInputList").val();
            var res = str.split(",");
            res.forEach(function(element) {
                lsttags= lsttags + "["+element+"]";

            });

            var form = new FormData();
            form.append("intitule",  "\"" + document.getElementById("inputIntitule").value + "\"" );
            form.append("description",  "\"" +document.getElementById("inputDescription").value+ "\"");
            form.append("id_utilisateur",  "2");
            form.append("tags", "\"" +lsttags+ "\"");

            var settings = {
               "async": true,
               "crossDomain": true,
               "url": "http://localhost/FAQ/api.php?action=post_question",
               "method": "POST",
               "headers": {
                 "cache-control": "no-cache",
                 "Postman-Token": "093d95af-25f5-4c51-bfcd-9f74577ad200"
               },
               "processData": false,
               "contentType": false,
               "mimeType": "multipart/form-data",
               "data": form
            }

            $.ajax(settings).done(function (response) {

                document.getElementById("inputIntitule").value="";
                document.getElementById("inputDescription").value="";
                $("#tagsInputList").tagsinput('removeAll');
                $("#myModalAddQuestion").modal("hide")
                location.reload();

                });

        } else {
            alert("Veuillez remplir tout les champs!");
        }
    };

My php API:

//je passe en paramètre mon objet PDO précédemment créé afin d'exécuter ma requête
function edit_question($pdo) { 
    $sql = "UPDATE `question` SET `intitule`='".$_POST["intitule"]."',`description`='".$_POST["description"]."' ,`tags`='".$_POST["tags"]."' WHERE `id`=".$_POST["id"];
    //création de la requête Sql pour aller chercher tous les articles
    $exe = $pdo->query($sql); 
    return $_POST["intitule"];
}

In my document.getElementById("inputIntitule").value i have Pourquoi votre site indique-t-il plusieurs disponibilités ?

but in $_POST["intitule"] i have Pourquoi votre site indique-t-il plusieurs disponibilit\u00c3\u00a9s ?

can you tel me why?

AndreaT
  • 367
  • 2
  • 10
  • looks like an encoding (utf-8) issue! The é is encoded to `\u00c3\u00a9` – Jeff Oct 26 '18 at 13:57
  • have a look at https://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Jeff Oct 26 '18 at 13:58
  • I see somewhere, if i use FormData, it's automaticaly in utf8.. – Anthony Careddu Oct 26 '18 at 14:17
  • 1
    I doubt that. It depends on html headers, depends later on encoding of php script, database and table charset, and so on. – Jeff Oct 26 '18 at 14:19
  • 1
    @andreat Please take better care when making edits. Your edit was questionable at best, and IMO actually harmful in that it rolled back some helpful changes. – Patrick Q Oct 26 '18 at 14:29
  • @PatrickQ Sorry but honestly I do not understand where I was wrong. I think I have edited it carefully. However I will pay more attention next time. – AndreaT Oct 30 '18 at 19:23

0 Answers0