Saturday 28 December 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...