I am building a rather large form. At one point users can upload an image.
This image should be uploaded immediately. Then users should continue to fill out the form.
HTML
<form name="registration" id="registration" action="confirm.php" method="post">
<input name="firstname" id="firstname" type="text" />
<input name="lastname" id="lastname" type="text" />
<!-- file upload -->
<input name="file" id="file" type="file" />
<input name="newsletter" type="checkbox" id="newsletter" />
<input name="captcha" id="captcha" type="tel" />
</form>
Javascript (jQuery)
//upload file on change event
$('#file').change(function() {
// put the image object into a variable
// formData is incomaptible with IE9/8/7
fileData = new FormData();
fileData.append('file', this.files[0]);
var options = {
// XMLHttpRequest Level 2 - no cross-browser support
data: fileData,
url: 'upload.php', // override standard url for this call only
type: 'post'
};
// make an ajax call to post the image
$.ajax(options);
// Alternatively use the jQuery Form plugin
// $('#registration').ajaxSubmit(options);
});
Unfortunately the jQuery Form Plugin http://malsup.com/jquery/form/#file-upload submits the entire form, when I want the input file field to be submitted only.
Also, I'd rather avoid building multiple individual forms in my HTML markup, as I'd then have to process and submit multiple forms as well.
What am I missing here?
-> should be <!-- file upload-->