-1

I'm currently trying to hammer together an image-uploading feature for a site I'm working on, and desire to do it using AJAX so as to not reload the page.

However, I've run into a snag. I'm basing myself off this question's answer, and in a previous project I got it to work. However, this time around the filedata does not seem to get stuffed into the formdata whatever I do. AJAX doesn't go through. Etc.

The jQuery looks like this:

$("body").on("change","#review_submit #imagefile",function() {
    console.log(this.files[0]);

    var formData = new FormData($("form#imageupload")[0]);
    console.log(formData);

    $.ajax({
        url: "php/submit-imageupload.php",
        type: "post",
        success: completeHandler = function(data) {
            if(data == "success") {
                console.log("Success!");
            }
        },
        error: function() {
            console.log("Oops");
        },
        // Form data
        data: formData,
        //Options to tell JQuery not to process data or worry about content-type
        cache: false,
        contentType: false,
        processData: false
    }, 'json');
});

The console log winds up looking like:

File { size: 1407970, type: "image/png", name: "powerTreads.png", path: "", lastModifiedDate: Date 2014-08-08T21:49:19.477Z, mozFullPath: "E:\Pictures\powerTreads.png" }
FormData {  }

Trying to browse the formdata there is no content to mention at all either.

The HTML looks like this: Image link of HTML

So can anyone see where I've gone wrong? :/

Community
  • 1
  • 1
Drunkenvalley
  • 126
  • 1
  • 2

1 Answers1

0

My god, I feel dumb now. The code wasn't the problem in the first place, but the PHP file I was sending to. At its destination I still had some features from the previous project that are no longer applicable (and don't exist, thus throwing an error I didn't see).

Just changed it to

success: completeHandler = function(data) {
    if(data == "success") {
        console.log("Success!");
    }
    else {
        console.log(data);
    }
},

and it got back to me about the problem with quite the description. (Wrong PDO file, SQL statements that no longer work because they try to insert into tables that don't exist, etc...)

EDIT: Do note however that it seems FormData will always look empty when trying to read its content through console.log(). So the content was, apparently, there all along.

Drunkenvalley
  • 126
  • 1
  • 2