dimanche 6 juin 2021

Access FormData Values

I'm trying to send an XHR request to a PHP page with several parameters (2 string, 1 blob) but I am unnable to get theses values in the PHP code..

here is my code :

JS

function initRequeteUpdImg(blob, file)
{
    img = document.getElementById("id");

    img.src = blob;

    requeteUdpRec = new XMLHttpRequest();
    
    requeteUdpRec.onreadystatechange = callback_ReqUpdRec;
    requeteUdpRec.open("POST","function/Modify_action.php", true);
    requeteUdpRec.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    function readFile(event) 
    {
        var fileType = file.type;
        blob = blobUtil.arrayBufferToBlob(event.target.result, fileType);

        formData = new FormData();
        formData.append("id", id);
        formData.append("action", "updImg");
        formData.append("file", event.target.result);

        requeteUdpRec.send(formData);
    }

    var reader = new FileReader();
    reader.addEventListener('load', readFile);
    reader.readAsArrayBuffer(file)
}

PHP

if(isset($_POST["action"]) && $_POST["action"]=="updImg")`{
    $requete = "select eleve_id from eleves where person_id = ?";
    $req_args = array($_POST['id']);
    $req_type = "i";
    $res = mysqli_fetch_array(prepared_query($requete, $req_args, $req_type));

    $requete = "update signature SET signature_blob = '".$_POST["file"]."' where eleve_id = ".$res["eleve_id"];

    $res = unprepared_query($requete, $req_args, $req_type);
}

else
{
    http_response_code (404);
}`

I'm obviously doing something wrong but where..?

I'm new to JS so if you could detail it would be very nice ! Thanks for your help !




Aucun commentaire:

Enregistrer un commentaire