0
I am unable to send the image to a folder, using Ajax and PHP, the file name sends correctly, it is only the file even if it does not write to the folder, the folder already has permission chmod 777.
What I need to do in ajax so PHP can save the image to the folder?
$(document).ready(function () {
$('#form_cadastro').validate({ // initialize the plugin
rules: {
Nome: {
required: true,
},
SobreNome: {
required: true,
}
},
messages: {
required: "Campo obrigatório",
remote: "Please fix this field.",
email: "Por favor insira um email válido",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Por favor digite apenas números.",
digits: "Please enter only digits.",
equalTo: "Please enter the same value again.",
maxlength: $.validator.format( "Não insira mais do que {0} caracteres." ),
minlength: $.validator.format( "Digite pelo menos {0} caracteres." ),
rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ),
range: $.validator.format( "Please enter a value between {0} and {1}." ),
max: $.validator.format( "Please enter a value less than or equal to {0}." ),
min: $.validator.format( "Please enter a value greater than or equal to {0}." ),
step: $.validator.format( "Please enter a multiple of {0}." )
},
submitHandler: function (form) { // for demo
$(".resultado_form_cadastro").html('<div class="spinner"></div>');
var form = $('#form_cadastro');
var Logomarca = $('#Logomarca').prop('files')[0];
var form_data = new FormData();
form_data.append('Logomarca', Logomarca);
//alert(form_data);
$.ajax({
url: 'form_cadastro.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'POST',
success: function(php_script_response){
//alert(php_script_response); // display response from the PHP script, if any
// pegando os dados
}
}) .done(function(data){
$('.resultado_form_cadastro').fadeOut('slow', function(){
$('.resultado_form_cadastro').fadeIn('slow').html(data);
});
})
.fail(function(){
alert('Ajax Submit Failed ...');
});
return false; // for demo
}
});
});
Html form
<form method="post" action="javascript:;" id="form_cadastro" enctype="multipart/form-data">
<div class="form-group row">
<div class="col-sm-6">
<label for="Logomarca">Logomarca</label>
<input type="file" class="form-control" id="Logomarca" >
</div>
<div class="col-sm-6">
<label for="funcionamento">Horário de Funcionamento</label>
<input type="text" class="form-control" id="funcionamento" placeholder="Horário de Funcionamento">
</div>
</div>
</form
PHP
$file_name_logomarca = $_FILES['logomarca']['name'];
$file_size_logomarca =$_FILES['logomarca']['size'];
$file_tmp_logomarca =$_FILES['logomarca']['tmp_name'];
$file_type_logomarca=$_FILES['logomarca']['type'];
if($file_size_logomarca > 2097152){
$errors[]='File size must be less than 2 MB';
$desired_dir="uploads";
if(empty($errors)==true){
if(is_dir("$desired_dir/".$file_name_logomarca)==false){
move_uploaded_file($file_tmp_logomarca,"$desired_dir/".$file_name_logomarca);
}else{ //rename the file if another one exist
$new_dir="$desired_dir/".$file_name_logomarca.time();
rename($file_tmp_logomarca,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
It’s not AJAX that writes it’s PHP. What code are you using in the back end (PHP)?
– Valdeir Psr
@Valdeirpsr put the PHP code also in question
– Wagner Martins Bodyboard
Alter
form_data.append('Logomarca', Logomarca);
forform_data.append('logomarca', Logomarca);
– Valdeir Psr
so it is not recording and also does not receive the name of the image. just as I did it takes the name and does not save the image form_data.append('Logo', Logo);
– Wagner Martins Bodyboard