Upload with jQuery

0

I have following code :

$(document).ready(function(e) {
   
	$("form").on("submit", function() {

		 $.post ("upload.php", {
			 
			 fotos : $("input[type=file]")
			 
		 }, function(retorno){
           
				alert(retorno)	 
		   }
		  );

		  return false;
		
	});
	
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><formmethod="post" 
  enctype="multipart/form-data" >
    <input type="file" id="imagens" name="imagens[]" multiple /> <br />
    <input type="submit" value="Enviar" />
</form>

upload.php

<?php

  print_r($_FILES);

?>

This code has 2 problems:

A , loose and then reload and not for return false

B) How do I send the file field to jQuery but with all the images selected? Note: in the form that the information of the first image selected in the jQuery array is only coming.

    
asked by anonymous 24.05.2018 / 18:56

1 answer

1

Solution:

  $(document).ready(function(e) {
	 
	  $("form").on("submit", function() {

		var formData = new FormData(this); 
		
		$.ajax({
			url: "upload.php",
			type: 'POST',
			data: formData,
			beforeSend: function() {			   
			},
			success: function (data) {
			},
			cache: false,
			contentType: false,
			processData: false
		});
		
		return false;
		  
	  });
	  
  });
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script><formmethod="post"
  enctype="multipart/form-data" >
    <input type="file" id="imagens" name="imagens[]" multiple /> <br />
    <input type="submit" value="Enviar" />
</form>

upload.php

  print "<pre>";
  print_r($_FILES);
  print "</pre>";  
    
24.05.2018 / 19:20