1
People need a help to upload images with AJAX, PHP and Mysql It takes all the data right, even writes the image path in Mysql, but does not create the file in the destination folder, what I want is to move the image set in the destination folder, if I do the same PHP process but without AJAX, it does the normal upload and moves it to the directory. Anyway what I want is to be able to upload image, I’ve seen some examples on the net and tried to adapt in mine but it didn’t work, please help!!
FORM
<form id="form_funcionario" class="needs-validation" enctype="multipart/form-data" novalidate>
<div class="form-group">
<div class="row">
<div class="col-sm" align="center">
<img src="img/fotoPadrao.png" id="image_pre" class="card__profile" />
<input type="file" id="upload" class="preview form-control" name="pic" accept="image/*" />
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label for="validationCustom01" class="col-form-label">Nome:</label>
<input type="text" id="validationCustom01" name="txtNome" class="form-control" required />
<div class="invalid-feedback">
Campo obrigatório*
</div>
</div>
<div class="col-sm-6">
<label for="validationCustom02" class="col-form-label">Sobrenome:</label>
<input type="text" id="validationCustom02" name="txtSobreNome" class="form-control" required />
<div class="invalid-feedback">
Campo obrigatório*
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<label for="validationCustom03" class="col-form-label">RG:</label>
<input type="text" id="validationCustom03" name="txtRg" class="form-control" required />
<div class="invalid-feedback">
Campo obrigatório*
</div>
</div>
<div class="col-sm-6">
<label for="validationCustom02" class="col-form-label">Tipo de Acesso:</label>
<div class="input-group mb-3">
<select class="form-control" name="selectTipo" required>
<?php include 'processosPHP/montarOptionsA.php'; ?>
</select>
<div class="input-group-append">
<button type="button" class="btn btn-success">
<i class="fas fa-plus"></i>
</button>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<label for="validationCustom04" class="col-form-label">Login:</label>
<input type="text" id="validationCustom04" name="txtLogin" class="form-control" required />
<div class="invalid-feedback">
Campo obrigatório*
</div>
</div>
<div class="col-sm-6">
<label for="validationCustom05" class="col-form-label">Senha:</label>
<input type="password" id="validationCustom05" name="txtSenha" class="form-control" required />
<div class="invalid-feedback">
Campo obrigatório*
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<label for="validationCustom06" class="col-form-label">Email:</label>
<input type="email" id="validationCustom06" name="txtEmail" class="form-control" required />
<div class="invalid-feedback">
Campo obrigatório*
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancelar</button>
<button type="submit" id="botaoCad" name="cadastrarFuncionario" class="btn btn-success">Cadastrar</button>
</div>
</form>
AJAX
$('#form_funcionario').submit(function(e){
e.preventDefault();
var formulario = $(this);
var retorno = inseriFormulario(formulario);
function inseriFormulario(dados){
$.ajax({
type:"POST",
data:dados.serialize(),
url: "processosPHP/cadastrarFuncionario.php",
async:false
}).then(sucesso,falha);
function sucesso(data){
$sucesso = $.parseJSON(data)["sucesso"];
$("#mensagem").show();
if($sucesso){
$("#mensagem p").html($.parseJSON(data)['mensagem']);
}else{
$("#mensagem p").html($.parseJSON(data)['mensagem']);
}
}
function falha(){
console.log("erro");
}
}
});
PHP
if(isset($_POST['txtNome'])) {
$nome = utf8_decode($_POST['txtNome']);
$sobreNome = utf8_decode($_POST['txtSobreNome']);
$rg = utf8_decode($_POST['txtRg']);
$email = utf8_decode($_POST['txtEmail']);
$login = utf8_decode($_POST['txtLogin']);
$senha = utf8_decode($_POST['txtSenha']);
$codTipoLogin = utf8_decode($_POST['selectTipo']);
$ext = strtolower(substr($_FILES['pic']['name'],-5)); //Pegando extensão do arquivo
$new_name = date("Y.m.d-H.i.s") . $ext; //Definindo um novo nome para o arquivo
$dir = './img/'; //Diretório para uploads
echo move_uploaded_file($_FILES['pic']['tmp_name'], $dir.$new_name);
$inserir = "INSERT INTO tbFuncionario ";
$inserir .= "(nome,sobreNome,rg,login,senha,foto,email,codTipoLogin) ";
$inserir .= "VALUES ";
$inserir .= "('$nome','$sobreNome','$rg', '$login','$senha','$new_name','$email','$codTipoLogin')";
$retorno = array();
$op_inser = mysqli_query($conecta,$inserir);
if($op_inser){
$retorno['sucesso'] = true;
$retorno['mensagem'] = "Transportadora inserida com sucesso";
}else{
$retorno['sucesso'] = false;
$retorno['mensagem'] = "Falha no sistema";
}
echo json_encode($retorno);
}
this manual will solve your problem partly https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485
– user60252
I already formatted him man
– Mauricio Dantas
So I can get all these in the code?
– user60252
I forgot to take them out, just to see if I was getting the data
– Mauricio Dantas
You can post the form?
– user60252
can yes, just a moment
– Mauricio Dantas
Note that I put ( enctype="Multipart/form-data" ) but still does not send via AJAX
– Mauricio Dantas
take a tour https://answall.com/tour
– user60252