0
I’m trying to make a upload of images with AJAX and PHP, but I get no success and do not find the error.
My code is:
$('.arquivo').change(function() {
var fileName = $(this)[0].files[0].name;
var formData = new FormData($('.photo_change'));
$('#modal_photo_content form img').show();
$.ajax({
url: 'http://localhost/photo_change.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(data) {
alert(data);
},
error: function() {
alert("ERRO: Tente novamente mais tarde.");
}
});
});
<form action="" method="post" enctype="multipart/form-data">
<input class="arquivo" name="img" type="file" />
<input type="submit" class="img_envia" name="envia_img" value="SELECIONAR IMAGEM" />
</form>
And PHP
<?php
session_start();
if(isset($_FILES['img'])){
require('connection.php');
$id = $_SESSION['login_id'];
$extensoes = array('png','jpg','jpeg');
$busca_dados_user = mysqli_query($conexao,"SELECT * FROM users WHERE id = '$id'");
if(mysqli_num_rows($busca_dados_user) == 1){
}
}else{
echo "Erro [003]: Faltam dados para a requisição.";
}
?>
But always returns: Erro [003]: Faltam dados para a requisição.
There’s something missing or wrong?
A while ago, I was like this same problem. I switched the isset for ! Empty and put a function in Ajax and it worked. I’ll put in the code to see if it helps you.
– Luizinho
You have to pass a native DOM form object and a jQuery selector always returns an array. Try this: var formData = new Formdata($('. photo_change')[0]);
– Fernando Nunes
Edited response!!
– user60252
What deja vu! I saw this question there and could not find my answer, but after all it is a different question! https://answall.com/questions/309755/upload-ajax-e-php/309765
– Leite
Then it had already been done! And I waste time answering this one from here. I saw that there are some differences.
– user60252