Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php doesn't parse data from multipart/form-data form's data sent by ajax

I have this form in html that has both file and other input elements together:

<form action="/imsick/disease/add" id="addDisease" method="post" role="form" data-async="" enctype="multipart/form-data">
<input type="text" id="name" name="name" class="form-control">
<textarea id="description" name="description" class="form-control" rows="10"></textarea>
<input type="checkbox" name="category[]" value="3">
<input type="checkbox" name="category[]" value="5">
<input type="checkbox" name="category[]" value="6">
<input type="checkbox" name="category[]" value="4">
<input type="checkbox" name="category[]" value="1">
<input type="checkbox" name="category[]" value="7">
<input type="checkbox" name="category[]" value="2">
<input type="file" id="approach-file" name="approach">

<input type="checkbox" name="symptom[0][is_patogonomic]">
<input type="text" name="symptom[0][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/symptom/search">

<input type="checkbox" name="symptom[1][is_patogonomic]">
<input type="text" name="symptom[1][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/symptom/search">

<input type="text" name="lab_symptom[0][name]" class="form-control typeahead" typeahead-source="http://localhost/imsick/labsymptom/search">
<select name="lab_symptom[0][anomaly_type]" class="form-control">
    <option value="ABOVE">ABOVE</option>
    <option value="UNDER">UNDER</option>
    <option value="POSITIVE">POSITIVE</option>
    <option value="NEGATIVE">NEGATIVE</option>
    <option value="++">++</option>
</select>
<input type="submit" class="btn btn-default center-block">
</form>

and this is my javascript/jquery code for submiting form data by ajax :

$('form[data-async]').on('submit', '', function(event) {
//form data object
event.preventDefault();
var $form = $(this);
var __fd = new FormData($form[0]);
$.ajax({
    type: $form.attr('method'),
    url: $form.attr('action'),
    data: __fd,
    processData: false,
    beforeSend: function(xhr) {
        //not important
    },
    success: function(data, status) {
        data = $.parseJSON(data);
        if (data.success) {
            //not important

        } else {
            //not important
        }
    },
    error: function() {
        //not important
    }
});
return false;
});

after submitting data , executing var_dump($_POST) on the form's target URL , will get me the following :

        array(2) {
      ["_url"]=>
      string(12) "/disease/add"
      ["------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition:_form-data;_name"]=>
      string(1404) ""name"

    asdasdaasdasdasdasda
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="description"

    asdsad
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="category[]"

    5
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="category[]"

    4
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="category[]"

    1
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="category[]"

    7
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="approach"; filename=""
    Content-Type: application/octet-stream


    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="symptom[0][is_patogonomic]"

    on
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="symptom[0][name]"

    asdasd
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="symptom[1][is_patogonomic]"

    on
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="symptom[1][name]"

    asdasd
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="lab_symptom[0][name]"

    asdasdasd
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN
    Content-Disposition: form-data; name="lab_symptom[0][anomaly_type]"

    ABOVE
    ------WebKitFormBoundaryHmT1pwGJDYbVxvrN--
    "
    }

it seems like php doesn't parse this data and $_POST array only has two members. could anyone tell me what am i doing wrong?

thanks

like image 706
HatsOn Avatar asked Jan 29 '26 18:01

HatsOn


1 Answers

Try to add contentType: false

$.ajax({
    type: $form.attr('method'),
    url: $form.attr('action'),
    data: __fd,
    processData: false,
    contentType: false ,  // tell jQuery not to set contentType
    beforeSend: function(xhr) {
   //not important
    },
    success: function(data, status) {
    data = $.parseJSON(data);
    if (data.success) {
        //not important

    } else {
        //not important
    }
},
error: function() {
    //not important
}

});

like image 76
Jithin Lukose Avatar answered Jan 31 '26 06:01

Jithin Lukose



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!