Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
How to access FormData sent with javascript vanilla in PHP

I have this simple program that saves canvas into blob file, I am trying to retrieve it in php, but I can't manage to access it.

My console.log(FormData) does show the blob file content properly.

HTML:

<form id="formId">
    <button id="click-photo" type="submit" value="submit">Click Photo</button>
    <canvas id="canvas" width="320" height="240"></canvas>
</form>

JavaScript:

form.addEventListener('submit', function(e) {

    e.preventDefault();
    
    var xhr = new XMLHttpRequest();
    var image_data_url = "";
    xhr.open('POST', 'image.php', true);
    if(xhr.readyState === XMLHttpRequest.DONE) {
        var status = xhr.status;
        if (status === 0 || (status >= 200 && status < 400)) {
            alert(xhr.reponseText);
        };
    };
    canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
    image_data_url = canvas.toDataURL('image/jpeg');
    var file = dataURLtoBlob( canvas.toDataURL('image/jpeg') );;
    
    const fd = new FormData;
    fd.append('image', file);
    xhr.send(fd);
});

PHP:

<?php
    if (isset($_FILES['image'])){
        print_r ($_FILES['image']);
    };
?>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are adding the image with a different name than the one you are trying to retrieve.

form.addEventListener('submit', function(e) {

    e.preventDefault();
    
    var xhr = new XMLHttpRequest();
    var image_data_url = "";
    xhr.open('POST', 'image.php', true);
    xhr.onreadystatechange = function ()
    {
        if(xhr.readyState === XMLHttpRequest.DONE) {
            var status = xhr.status;
            if (status === 0 || (status >= 200 && status < 400)) {
                alert(xhr.reponseText);
            };
        };
    }

    canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
    image_data_url = canvas.toDataURL('image/jpeg');
    var file = dataURLtoBlob( canvas.toDataURL('image/jpeg') );;
    
    const fd = new FormData;
    fd.append('image', file);
    xhr.send(fd);
});
// ...
if (isset($_FILES['file'])){
// Up here it should be: if (isset($_FILES['image'])){
// ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!