0

I need to get the uploaded image using PHP via Ajax

My form fields are,

<form action="#" method="post" id="rent_details" name="rent_details" enctype="multipart/form-data">
Upload Image :<input type="file" name="fileToUpload" id="fileToUpload">
type:- <select name="spottype" id="spottype">
  <option value="xxx">xxx</option>
  <option value="yyy">yyy</option>
  <option value="zzz">zzz</option>
</select> 
<input type="button" id="bidm" name="bidm" value="Next"/>
</form>

In ajax call I have following code :-

   $.ajax({
        url: './api/addspot.php',
        data: $('#rent_details').serialize(),
        type: 'POST',   
         contentType: false,                
         success: function(data) {
            alert(data);
        },
        error: function(xhr, status, error) {
         alert(xhr.responseText);
        }
    });   

Here I got only spottype value in Ajax success function .But I need to get all form fields value.

soniya soniya
  • 297
  • 2
  • 3
  • 14

5 Answers5

1

This will work.

$('#rent_details').on('submit',(function(e) {
    e.preventDefault();
    var formData = new FormData(this);

    $.ajax({
      url: './api/addspot.php',
      data: formData,
      type: 'POST',   
      contentType: false,
      processData: false,            
      success: function(data) {
        alert(data);
      },
      error: function(xhr, status, error) {
       alert(xhr.responseText);
      }
  });   

}));
Mihir Bhatt
  • 3,019
  • 2
  • 37
  • 41
1
<form action="#" method="post" id="rent_details" name="rent_details" enctype="multipart/form-data">
Upload Image :<input type="file" name="fileToUpload" id="fileToUpload">
type:- <select name="spottype" id="spottype">
  <option value="xxx">xxx</option>
  <option value="yyy">yyy</option>
  <option value="zzz">zzz</option>
</select> 
<input type="submit" id="bidm" name="bidm" value="Next"/>
</form>

$(document).ready(function(){
    $("#rent_details").submit(function(e){
        e.preventDefault();
        $.ajax({
                method:'POST',
                url: "./api/addspot.php",
                data: new FormData( this ),
               processData: false,
                contentType: false
                }).done(function(data) {
                    console.log(data);
            });
    });
});

And get by name on your requested page. use $_FILES for upload files.

Jitendra
  • 558
  • 8
  • 23
0

You need to use the FormData();

Know that this code will not work on IE9 and lower.

This is for multiple file upload.

    $(document).on('click', '#bidm', function(e) {
        e.preventDefault();

        var request = new FormData(this);
        var my_files = $(this).closest('form').find('#fileToUpload').files;

        $.each(my_files, function(j,file) {
`              request.append('fileToUpload['+j+']', file);
        });

        $.ajax({
             url: './api/addspot.php',
             data: request,
             dataType: "json",
             contentType: false,
             processData: false,
             enctype: 'multipart/form-data',            
             success: function(data) {
                 alert(data);
             },
             error: function(xhr, status, error) {
                 alert(xhr.responseText);
             }
        });
    });
Codew
  • 474
  • 4
  • 16
0

Need to use the FormData() for the ajax data. It took all the form variables and passed it to the ajax. Then in the ajax function, you can retrieve the file name and the temp file name and save the image file to the folder where you required to save.

Sujitha
  • 19
  • 8
-1

Try this to get file field value:

$('input[type=file]').val()

This code is worked

Community
  • 1
  • 1
Raheel
  • 8,716
  • 9
  • 60
  • 102