Keep values in the fields during Jquery / PHP validation

0

I have a form which I am validating using jQuery / PHP. After 50 years of using it, I realized that during validation the field values are erased. How would I keep the values in the fields during validation? Look at 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>
    
asked by anonymous 16.05.2017 / 00:31

1 answer

2

Speak my friend, what you have to do is the following. In the send.php you will do the validation checks, if there is any exception you to the execution of the code and returns a json, example below:

if(empty($_POST['nome']){  
  echo json_encode(
              array(
                 "error" => true, 
                 "mensagem" => "O campo nome não pode ser vazio"));
  exit;
}

This, in your jQuery code, you look for this error.

$.post("enviar.php", $("#contact-form").serialize())
.done(function(response){
//se não houver erro de validação ele limpa os campos e carrega o response.
  if(!response.error){
     $('#success').html(response);
     $('#nome').val('');
     $('#email').val('');
     $('#assunto').val('');
     $('#mensagem').val('');
  }else{
     alert(response.mensagem)
  }
})
.fail(function(xhr, status, error){
  //tratar falha do server
})
    
16.05.2017 / 07:11