0
I have a form that I validate using jQuery/PHP. After 50 years using, I realized that during the validation the values of the fields are erased. How would I keep the values in the fields during validation? See the code:
HTML
<form method="post" name="form" novalidate="" id="contact-form">
<div id="success"></div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="Nome" class="form-control" placeholder="Nome *" id="nome" required="required">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="email" name="Email" class="form-control" placeholder="E-mail *" id="email" required="required" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="Telefone" class="form-control" placeholder="Telefone *" data-inputmask="'mask' : '(99)9999-9999'" id="telefone" required="required">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" name="Celular" class="form-control" placeholder="Celular *" data-inputmask="'mask' : '(99)99999-9999'" id="celular" required="required">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea name="Mensagem" class="form-control" placeholder="Mensagem *" id="mensagem" required="required"></textarea>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="row">
<div class="col-lg-12 text-center">
<button type="submit" id="submit" class="btn">Enviar</button>
</div>
</div>
</form>
JQUERY
<script type="text/javascript">
$('#submit').click(function() {
$.post("enviar.php", $("#contact-form").serialize(), function(response) {
$('#success').html(response);
$('#nome').val('');
$('#email').val('');
$('#assunto').val('');
$('#mensagem').val('');
});
return false;
});
</script>
Why are you cleaning the value of inputs?
$('#nome').val('');
and the others– KillerJack
What is the code of
enviar.php
? Is this where you do the validation? If it is, it doesn’t make much sense for you to clear the values of the fields as Killerjack commented.– Woss
You are answering me with questions that are the same as I expect the answers r... That’s why I posted, because I need help in code ;). But come on. First question. I clean the values after the email is sent, but I saw that it is wrong. Second and third question. The sending code does the validation, but it is not the problem in it, but in jquery. The goal of clearing the fields is after sending, but as I said, after a while I went to see that was wrong and so I am here.
– user24136