janoliver
janoliver

Reputation: 7824

jQuery: Create and submit a form with file upload

I have a simple field somewhere in my document. Now I want to CREATE a form with jQuery, add this input field to it and submit it via Javascript.

Something like this:

var newform = $( document.createElement('form') );
newform.attr("method","post")
       .attr("action",action)
       .attr("enctype","multipart/form-data");
       .append($("#file").clone())
       .submit();

Unfortunately, $_FILES gives me error code 4: "No file submitted". I tried this with simple Text fields, and it worked for them, their value has been submitted too. Just the file-upload won't work.

Any suggestions how that could be done?

Upvotes: 2

Views: 3869

Answers (1)

czarchaic
czarchaic

Reputation: 6318

I think it has to do with the fact that $("#file").clone() would cause the input appended to the form to have the same id '#file'.

Upvotes: 1

Related Questions