1
I have a form where through the modal Bootstrap, it automatically uploads. It’s working properly, the problem is it’s not clearing the field. Look:
<form method="post" id="form-upload" novalidate enctype="multipart/form-data">
<label for="email" style="font-weight: normal"><strong>Formato permitido:</strong> JPG, JPEG e PNG<br><strong>Tamanho da imagem:</strong> 1170 x 300</label>
<div class="md-group-add-on">
<span class="md-add-on-file">
<button class="btn btn-primary waves-effect waves-light">Foto</button>
</span>
<div class="md-input-file">
<input type="file" id="fotoCapa" name="FotoCapa"/>
<input type="text" class="md-form-control md-form-file">
<label class="md-label-file"></label>
</div>
</div>
<div id="sucesso"></div>
</form>
<script type="text/javascript">
$(function(){
$('#fotoCapa').change(function(e) {
e.preventDefault();
var formData = new FormData();
formData.append('FotoCapa', $('#fotoCapa').prop('files')[0]);
$.ajax({
url: 'alterar-foto-capa.php',
data: formData,
type: 'post',
success: function(response){
var status = JSON.parse(response);
if(status.Status === 0){
$('#sucesso').html("<div class='alert alert-success'>A foto foi alterada com sucesso!</div>");
$('#fotoCapa').val("");
}else{
$('#sucesso').html("<div class='alert alert-danger'>" + status.Status + "</div>");
}
console.log(response);
},
processData: false,
cache: false,
contentType: false
});
});
});
</script>
You’re telling me to clean up
#fotoCapa
only ifstatus.Status === 0
, you checked if this condition is valid?– Leandro Angelo
Hello Leandro. Yes, so much so that when the condition is valid, it activates Alert-Success.
– user24136
but what you say doesn’t clear is actually the #fotoCapa input or the second input text below it?
– Leandro Angelo
Just want to clear that field? Because if it’s all fields, it’s easier to reset the form like this
$('#form-upload')[0].reset();
or so$('#form-upload').trigger("reset")
, That’s clear if the conditionstatus.Status === 0
is true– Ricardo Pontual
Perfect Ricardo. It worked with
$('#form-upload')[0].reset();
Thank you!– user24136
Sorry Leandro. I hadn’t seen your message. That would be just #fotoCapa, but Ricardo’s solution worked. Thank you.
– user24136