0

This is my code

                        $.ajax({
                            type: 'post',
                            url: '../lib/upload.php',
                            data: new FormData( $("#niceidentifier") ),
                            processData: false,
                            contentType: false,
                            success: function (response) {
                                if(response == 'success') {
                                    return true;
                                }
                                else {
                                    alert(response);
                                    console.log(response);
                                }
                            }
                        });

The HTML form is just basic HTML (enctype included and method post) but unfortunately NO data is passed. How can I upload a file AND pass the input data once?

3
  • @Kami so I have to do "form_data.append" for every single input field? Even if it's not a file? Commented Nov 18, 2014 at 17:49
  • #niceidentifier is the id of the form ? Commented Nov 18, 2014 at 17:50
  • @SebriZouhaier no, thats the form's ID Commented Nov 18, 2014 at 17:53

4 Answers 4

0

It's not as simple to pass files and other data, such as text, together in the same POST request. The only way to achieve that is making multipart/form-data request.

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

Sign up to request clarification or add additional context in comments.

Comments

0

I use ajaxForm to upload files asynchronously via ajax, it is much easier than trying to implement your own.

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

Comments

0

For a cross browser solution I think it is better to use

$("#form_id").ajaxSubmit();

Comments

0

You can use FormData to pass the File. and processData,contentType set to false compulsory. because you are not going to process file in client side.

// Create a formdata object and add the files
var data = new FormData();
$.each(files, function(key, value)
{
        data.append('myFile', value);   //No I18N
});


$.ajax({
    url: '/your-url', 
    type: 'POST',
    data: data,
    cache: false,
    dataType: 'json',   //No I18N
    processData: false, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    success: function(data, textStatus, jqXHR)
    {
    // do something
    },
    error: function(jqXHR, textStatus, errorThrown)
    {
            // Handle errors here
    }
});

OR you can send file with data as following:

$( '#formId' )
  .submit( function( e ) {
 e.preventDefault();
 $.ajax({
    url: '/your-url', 
    type: 'POST',
    data: new FormData( this ),
    cache: false,
    dataType: 'json',   //No I18N
    processData: false, // Don't process the files
    contentType: false, // Set content type to false as jQuery will tell the server its a query string request
    success: function(data, textStatus, jqXHR)
    {
    // do something
    },
    error: function(jqXHR, textStatus, errorThrown)
    {
            // Handle errors here
    }
});
});

6 Comments

And how does the PHP script gets my input type text content?
@user3613095 I am not too much familiar with php but i think you can access it using $_FILES['myFile']['tmp_name']. you can read it w3schools.com/PHP/php_file_upload.asp
I get the FILE but I dont get the 20 TEXT, SELECT and RADIO FIELDS I also have in my form.
@user3613095 you need to append those values too. like data.append('name', some-value);
Cant that happen automatically? @kriyeta? My forms are more complex and it can variate between field types...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.