Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload without Form

Without using any forms whatsoever, can I just send a file/files from <input type="file"> to 'upload.php' using POST method using jQuery. The input tag is not inside any form tag. It stands individually. So I don't want to use jQuery plugins like 'ajaxForm' or 'ajaxSubmit'.

like image 775
Elo Peri Avatar asked Oct 27 '13 12:10

Elo Peri


People also ask

How can I upload a file without form?

Step 1: Create HTML Page where to place the HTML Code. Step 2: In the HTML Code Page Bottom(footer)Create Javascript: and put Jquery Code in Script tag. Step 3: Create PHP File and php code copy past.

How can I upload a picture without form?

By using PHP Script we can upload selected file or Image to server without refresh of page. After done uploading selected Image we have also uploaded image on web page without refreshing of page. So this is our simple tutorial on How to Upload Image without using Form Submit in Ajax PHP.


1 Answers

You can use FormData to submit your data by a POST request. Here is a simple example:

var myFormData = new FormData(); myFormData.append('pictureFile', pictureInput.files[0]);  $.ajax({   url: 'upload.php',   type: 'POST',   processData: false, // important   contentType: false, // important   dataType : 'json',   data: myFormData }); 

You don't have to use a form to make an ajax request, as long as you know your request setting (like url, method and parameters data).

like image 129
Omid Monshizadeh Avatar answered Oct 04 '22 17:10

Omid Monshizadeh