0

How can I easily implement "formData" in this ajax call?

HTML:

<form id="insertDocument" action="#" enctype="multipart/form-data">
    <input type="file" id="file" name="file" />
    <select name="type_d" id="type_d">
        <option value="1">something</option>
        <option value="2">something</option>
        <option value="3">something</option>
        <option value="3">something</option>
        <option value="3">something</option>
    </select>
    <input type="text" name="description_d">
    <input type="date" name="date_d">
    <input type="date" name="valid_d">
    <input type="submit" value="Upload document" id="insert_d">
</form>

jQuery:

$("#insert_d").click(function() {
    event.preventDefault();
    var data = $("#insertDocument").serialize();
    $.ajax({
        type: 'POST',
        url: 'insertDocument.php',
        data: data,
        success: function(html){
            console.log(html);
        }
    });
});

My problem is, that I need to post all the data - file and other inputs. Do I have to combine formData with serialize?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
marc_s
  • 1,007
  • 3
  • 14
  • 24
  • I need an example of implementing this particular solution in my case. Maybe the content is duplicate, but solution is not. – marc_s Dec 02 '14 at 10:39
  • 1
    May this link help you : http://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax – Sadikhasan Dec 02 '14 at 10:40

0 Answers0