2
I have a form with type fields text
and file
:
<form id="first-setup" method="post" enctype="multipart/form-data" data-toggle="validator">
<input type="hidden" id="userkey" value="{userKey}">
<label for="input-firstname">Nome</label>
<input id="input-firstname" name="input-firstname" data-minlength="2" data-error="Desculpe, seu nome deve possuir no mínimo 2 dígitos." type="text" class="form-control" required>
<label for="input-surname">Sobrenome</label>
<input id="input-surname" name="input-surname" data-minlength="2" data-error="Desculpe, seu sobrenome deve possuir no mínimo 2 dígitos." type="text" class="form-control" required>
[...]
<label for="input-picture">Foto de perfil</label>
<input id="input-picture" name="input-picture" type="file" class="filestyle" data-iconName="fa fa-spaced fa-folder-open-o" data-input="false" data-buttonText="Envie uma foto de perfil">
<button id="button-submit" class="btn btn-warning" type="submit">Concluir <i class="fa fa-spaced fa-arrow-circle-o-right"></i></button>
</form>
I use ajax to send it:
$('#button-submit').click(function(e) {
e.preventDefault();
// Correção de bug do bootstrap-validator
// Bug relatado: envia o formulário mesmo com o input[submit] desativado (disabled=true) - ou seja
// com erros de validação nos valores.
var disabled = $(this).hasClass('disabled');
if (disabled) return false;
var firstname = $('#input-firstname').val(),
surname = $('#input-surname').val(),
fullname = $('#input-fullname').val(),
birthdate = $('#input-birthdate').val(),
city = $('#input-city').val(),
bgfile = $('#input-background').val(),
picfile = $('#input-picture').val();
// validações e eventos na view
var form = $('#first-setup').serialize();
$.ajax({
url: '../application/controller/class.FirstSetupController.php',
dataType: 'json',
data: form,
})
.done(function(data) {
console.log(data);
})
.fail(function(data) {
console.log(data);
});
});
The problem is that in php, if I make one if (isset($_FILES["input-picture"]))
he returns me false
, only recognizing the input[type=text]
form.
I already checked the php settings (file_uploads
, post_max_size
and upload_max_file_size
) and they’re okay, man .htaccess
also.
I tried to use the solutions of other similar questions but none solved this problem.
I think the
$.ajax
does not upload, you have to use theFile API
.– Guilherme Nascimento