Upload an image with ajax without using a form

Lelio Faieta

I know that form nesting is not a valid option for html5 (and generally for html) so I'd like to understand how can I do a file upload inside a form without using another form.

I have a form where the user can choose one image from a dropdown. I'd like to give the user the ability to add another image on the fly while filling the form. The new image will be then added as an option to the dropdown and the user can choose it.

EDIT: when I say "choose an image from a dropdown" I mean that the images are stored on the server on a specific folder. In the dropdown I show the files name (stored in a db). Adding a new image to the folder will add it to the db to and will add the new image name to the select. But each select option will just be:

<option value="id_from_db">Image_name_from_db</option>

and the table in the db will have: id - name - path_to_file

Usually I use the jquery Form plugin to upload the new image and this plugin looks for a form with the <input type="file"> tag to do the upload. Is there any chance to upload the image without the nested form? I was thinking about using an iframe but this looks like a crazy idea. The actual html structure looks like:

<form>
    //some more stuffs for the main form
    <select name="image">
        <option>existing options</option>
    </select>
    <form>
        <input type="file">
        <button>Upload file</button>
    </form>
    //some more stuffs for the main form
    <button>Submit form</button>
</form>

I have no issue posting the main form, I have no issue adding the new file as an option to the select. But this structure is not a valid html and will not work.

KWeiss

You can use the readAsDataURL method to display the image in the dropdown. Or just add an option that reads something like "use own image" after the user used the file input to upload an image.

Then you can just post the form as normal, containing both the user's image and the information that he wants to use it. Connecting the two bits of information will happen on the server side.

If you absolutely want to upload the image first, use AJAX. jQuery can get the value from the input without considering the rest of the form:

$(imageInput).on('change', function(){
    var data = this.files[0];
    $.post(imagePostUrl, data);
});

In the html, either put the imageInput outside your form, or remove the image input from your form data with Javascript when the form is submitted, if you don't want your image to be uploaded again.

Please note that this will only work in HTML5 compliant browsers. Older browsers can't send files via AJAX this way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

File upload using Jquery ajax without form

From Dev

Upload image using ajax and form submitting

From Dev

Upload image using ajax and form submitting

From Dev

Upload form with image in codeigniter using ajax

From Dev

upload image in form submit with ajax

From Dev

Combine image upload ajax with form submit ajax

From Dev

WordPress image upload Using ajax?

From Dev

Upload image with phonegap using ajax

From Dev

Upload Image using ajax (json)

From Dev

Upload image to imgur using ajax

From Dev

jQuery upload and preview image onchange through ajax without submitting the whole form

From Dev

Ajax file upload outside of form without plugins

From Dev

ajax upload image with another data in form

From Dev

codeigniter form upload with images using ajax

From Dev

Upload File in Spring without using form data

From Dev

Upload File in Spring without using form data

From Dev

upload image in form using node.js

From Dev

Upload multiple image using AJAX, PHP and jQuery

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

Laravel Multiple Image Upload using Ajax

From Dev

Image Upload in PHP using AJAX Jquery not working

From Dev

How to upload image without using submit button?

From Dev

can not upload image with other form fields in spring mvc 4 + ajax

From Dev

How can i upload an image to a node js server without a form?

From Dev

Form submitting using jquery without ajax in rails

From Dev

Ajax image using jquery form.js

From Dev

ajax upload file form

From Dev

codeigniter form with image upload

From Dev

Form submit to upload file and also other fields using ajax