2

I read some SO questions but nothing could help me.

I've a normal HTML form:

<form id="featured_upload" method="post" action="#" enctype="multipart/form-data">
  <input type="file" name="my_image_upload" id="my_image_upload" multiple="false" />
  <input type="hidden" name="post_id" id="post_id" value="<?php echo get_the_ID(); ?>" />
  <?php wp_nonce_field( 'my_image_upload', 'my_image_upload_nonce' ); ?>
  <div class="button-reg-box">
    <button type="submit" class="btn btn-primary btn-set-picture">Profilbild bearbeiten</button>
  </div>
</form>

This is my jQuery function:

jQuery('#featured_upload').submit(function (e) {
    e.preventDefault();
    SetAndChangePicture();
});

function SetAndChangePicture() {
    var test = new FormData(this); <-- empty :(
    console.log(test);
    jQuery.ajax({
        type: "POST",
        async: false,
        url: dmd_output_favorites_obj.ajax_url,
        data: {
            action: "dmd_picture",
            formdata: new FormData() <-- empty too but I want to test above
        },
        success: function (data) {
            //jQuery('#MemberAreaChangeInterests .alert-success').show();
        }
    });
}

This var test = new FormData(this); is everytime empty.

I tried this and the jQuery selector of the form. Both aren't working.

EDIT:

Error in console:

Uncaught TypeError: Illegal invocation

EDIT:

Here is a picture of my debug:

enter image description here

3 Answers 3

1

Try Following code :

var formData = new FormData();
formData.append('image', $('input[type=file]')[0].files[0]); 

Sending form

Ajax request with jQuery will looks like this:

$.ajax({
    url: 'Your url here',
    data: formData,
    type: 'POST',
    contentType: false,
    processData: false,
    //...Other options like success and etc
})
Sign up to request clarification or add additional context in comments.

2 Comments

I debug the var formData but this var is still empty.
@cgee should try remove SetAndChangePicture paste code there
0

Try this

var formData = $('#featured_upload').serialize();

This will give you the form details

1 Comment

It is still empty.
0

Try the following:

add a input type hidden with the action

<input type="hidden" name="action" id="post_id" value="dmd_picture" />

change data to:

 data: new FormData(jQuery('#featured_upload')[0]); 

use the following ajax properties

 contentType: false,
 processData: false,

Note: don't use async:false;

5 Comments

Still empty and this new error appears. Uncaught TypeError: k.toLowerCase is not a function
FormData does not accepts argument right ? developer.mozilla.org/en-US/docs/Web/API/FormData
what is empty? how do you know is empty?
I debugging with firebug... it is empty. $_FILE in PHP is also empty.
can you provide the php function for this ajax reuqest?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.