Post multipart/form-data using Ajax

Malcolm

For some reason I can not get the following script to work correctly, when submitting the form without using the script all works as it should, but when using the script to submit the form I only get the category and description in the post variables but no file. So my question is how do I get the script to post the file variable also.

Ajax

$("#img-post").click(function()
{
    $("#imgupload").submit(function(e)
    {
        var postData = $(this).serializeArray();
        var formURL = $(this).attr("action");
        $.ajax(
        {
            url : formURL,
            type: "POST",
            data : postData,
            success:function(data, textStatus, jqXHR) 
            {
                $("#img").html('<pre><code class="prettyprint">'+data+'</code></pre>');

            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                alert('Error');
                document.getElementById('enquiry').style.visibility = 'hidden';
            }
        });
        e.preventDefault(); //STOP default action
    });

    $("#imgupload").submit(); //SUBMIT FORM
});

HTML

<div class="img" id="img"></div>

<form name="imgupload" id="imgupload" action="upload.php" method="post" enctype="multipart/form-data">
<input name="category" id="category" type="text" />
<input name="file" id="file" type="file" />
<textarea name="discription" id="discription" cols="68%" rows="8"></textarea><br>
<input type="button"  id="img-post" name="img-post" value="Add" />
</form>
A.O.

Data from file select elements is not serialized

Taken from the docs page at:

https://api.jquery.com/serializeArray/



However, you can achieve this with the jquery ajax form plugin found here:

http://malsup.com/jquery/form/

This plugin is nice because not only does it capture the form data (including files) you can easily send extra $_POST data in the data attribute of the ajax call along with your form.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

POST data using AJAX POST

From Dev

post data to view using ajax

From Dev

post data to view using ajax

From Dev

How to upload a variable amount of files using a single multipartform post header key

From Dev

How to append data to specific post using ajax?

From Dev

Not able to post data using Ajax on server

From Dev

How to POST data to php file using AJAX

From Dev

PHP form passing post data using AJAX

From Dev

jmeter with post data using ajax or javascript

From Dev

PHP POST data to another page using AJAX

From Dev

Unable to read POST'd data using $.ajax

From Dev

How to POST data to php file using AJAX

From Dev

Post Data and Multiple Image using Ajax

From Dev

Retrieve array data on POST using PHP Ajax

From Dev

AngularJS lost data from multipartForm directive to routes

From Dev

iOS, upload file with multipartform-data

From Dev

AngularJS lost data from multipartForm directive to routes

From Dev

How to post data to database using Ajax call in Laravel?

From Dev

Testing an AJAX POST using Rack::Test - how to pass in data?

From Dev

How to post a form to PHP using Ajax .serialize() data?

From Dev

How to Post and insert Data into DB using Laravel, Bootstrap Modal and Ajax

From Dev

Sending an image and JSON data to server using Ajax POST request?

From Dev

How to get the data passed via AJAX and Jquery using $_POST?

From Dev

Post form data to Fw/1(Coldfusion) controller function using AJAX

From Dev

How to post data to ASP.NET MVC controller using AJAX?

From Dev

how to pass JSON data to php using ajax post

From Dev

How to POST two variable using ajax for save the data

From Dev

How to send data to post method of node js using AJAX/jQuery?

From Dev

Testing an AJAX POST using Rack::Test - how to pass in data?

Related Related

  1. 1

    POST data using AJAX POST

  2. 2

    post data to view using ajax

  3. 3

    post data to view using ajax

  4. 4

    How to upload a variable amount of files using a single multipartform post header key

  5. 5

    How to append data to specific post using ajax?

  6. 6

    Not able to post data using Ajax on server

  7. 7

    How to POST data to php file using AJAX

  8. 8

    PHP form passing post data using AJAX

  9. 9

    jmeter with post data using ajax or javascript

  10. 10

    PHP POST data to another page using AJAX

  11. 11

    Unable to read POST'd data using $.ajax

  12. 12

    How to POST data to php file using AJAX

  13. 13

    Post Data and Multiple Image using Ajax

  14. 14

    Retrieve array data on POST using PHP Ajax

  15. 15

    AngularJS lost data from multipartForm directive to routes

  16. 16

    iOS, upload file with multipartform-data

  17. 17

    AngularJS lost data from multipartForm directive to routes

  18. 18

    How to post data to database using Ajax call in Laravel?

  19. 19

    Testing an AJAX POST using Rack::Test - how to pass in data?

  20. 20

    How to post a form to PHP using Ajax .serialize() data?

  21. 21

    How to Post and insert Data into DB using Laravel, Bootstrap Modal and Ajax

  22. 22

    Sending an image and JSON data to server using Ajax POST request?

  23. 23

    How to get the data passed via AJAX and Jquery using $_POST?

  24. 24

    Post form data to Fw/1(Coldfusion) controller function using AJAX

  25. 25

    How to post data to ASP.NET MVC controller using AJAX?

  26. 26

    how to pass JSON data to php using ajax post

  27. 27

    How to POST two variable using ajax for save the data

  28. 28

    How to send data to post method of node js using AJAX/jQuery?

  29. 29

    Testing an AJAX POST using Rack::Test - how to pass in data?

HotTag

Archive