Wednesday, 23 August 2017

javascript - Pass an image through AJAX

Basically I want to pass a image file with ajax on submitting a form and retrieve the image and send it by email as an attachment file:




Here's the form :

























Parcourir

















I can't seem to find what's the error in my code ! Here's the AJAX call :




jQuery(document).on("click", "#submit", function(e) {
e.preventDefault();
var fileInput = document.getElementById('image_input_field');
var file = fileInput.files[0];
var formData = new FormData();
formData.append('file', file);
// console.log(file);

var societe = $("input#societe").val();

var message = $("textarea#message").val();
jQuery.ajax({
url: "ajax.php",
type: "post",
data: {
'file': file,
'module' : 'ajax_data_form',
'societe': societe,
'message': message
},

cache: false,

success: function(reponse) {
if(reponse) {
alert(reponse);
// console.log(reponse);
// jQuery('#devis').trigger("reset");
} else {
alert('Erreur');
}

}
});
});


And here's the ajax.php:



if( isset($_POST['module']) && $_POST['module'] == "ajax_data_form" )
{

var_dump($_FILES);
}

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...