Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input::all() empty after FormData object passed

I have a form that I need to process before it's submitted. The form has several text and radio inputs, as well as a file one. I'm using jQuery to pass the form data:

$button.on('click', function(e) {
    e.preventDefault();

    var formData = new FormData($('.js-my-form')[0]);

    $.ajax({
        type: 'GET',
        url: '/get-data',
        data: formData,
        contentType: false,
        processData: false
    }).done(function(response) {
        // handle response
    });

});

Then, in Laravel controller:

$input = Input::all() // <- EMPTY ARRAY

No idea why Input comes empty. Any help appreciated.

like image 316
lesssugar Avatar asked Mar 09 '26 03:03

lesssugar


1 Answers

Your problem is that you are using a GET to send a request with enctype = multipart/form-data. When you use a native FormData object this is the format that you get by default. You can read how to send the form's data

using the POST method and setting the enctype attribute to application/x-www-form-urlencoded (default);

using the POST method and setting the enctype attribute to text/plain;

using the POST method and setting the enctype attribute to multipart/form-data;

using the GET method (in this case the enctype attribute will be ignored).

So if you set your requesto to a GET the content of your form present in the body of the request will be ignored. The enctype attribute is what tells the server how to process your request. This is why you should use POST.

Note that when you write

contentType: false,
processData: false

you are telling jQuery that do not mess with the data you are passing and leave your native Formdata object untouched. This will cause the enctype to be set automatically.

like image 161
devconcept Avatar answered Mar 10 '26 15:03

devconcept



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!