Intereting Posts

php не анализирует данные из данных формы multipart / form-data, отправленных ajax

У меня есть эта форма в html, которая имеет как файл, так и другие элементы ввода вместе:

<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> 

и это мой код javascript / jquery для отправки данных формы с помощью 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; }); 

после отправки данных, выполняя var_dump($_POST) на целевом URL формы, вы получите мне следующее:

  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-- " } 

похоже, что php не анализирует эти данные, а массив $ _POST имеет только два члена. может ли кто-нибудь сказать мне, что я делаю неправильно?

благодаря

Попробуйте добавить 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 } 

});