AnchovyLegend
AnchovyLegend

Reputation: 12538

Ajax form submission results in empty POST array

I have spend so much time on trying to figure out why when I try to submit a basic form in an ajax request, the POST array is empty ? I have double and triple checked and it is simply not going through. Maybe I am missing something ?

I appreciate any advice!

HTML

<form id="csv_form" action="?" method="post" name="action" enctype="multipart/form-data">
    <input type="file" name="csv" class="csv" />
    <input type="submit" name="submit" value="Upload CSV" />
</form>

JQuery

$('#csv_form').submit(function(event){

    $.ajax({
        type: 'post',
        url: "import.leads.ajax.php",
        data: $(this).serialize(),
        success: function(html){
            alert(html);
         }
    });
});

import.leads.ajax.php

var_dump($_POST); //output array(0) { }

Upvotes: 0

Views: 441

Answers (1)

Brian Moreno
Brian Moreno

Reputation: 1027

You have to use formData to upload a file via ajax. Try reading this simple article on how to upload a file using ajax, it's super simple!

http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

Upvotes: 1

Related Questions