Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
My Ajax output is disappearing after one sec

I am using Ajax with form in Ajax I have used a if condition for validation if my form is empty it should display a message that all fields are required and if fields are fill it should add data in dB and display a success message although everything is working perfectly except one thing which is that my output only display for a second and then automatically disappears can anyone help me in this regard:

`

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Document</title>
  <script src="jquery.js"></script>
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
  <h1>PHP & Ajax Seralize Form</h1>
  <form id="form-data" method="post">
      Name<br><input type="text" name="name" id="name" value=""><br><br>
      Age<br><input type="number" name="number" id="age" value=""><br><br>
      Gender<br>
        <input type="radio" name="gender" value="Male">Male
        <input type="radio" name="gender" value="Female">Fe-male<br><br>

      <select name="country">
        <option value="Kashmir">Kashmir</option>
        <option value="Egypt">Egypt</option>
        <option value="Norway">Norway</option>
        <option value="Iceland">Iceland</option>
      </select><br>
      <br><input type="submit" id="submit" value="Save">
  </form>

<div id="response">

</div>

<script>
  $(document).ready(function(){
    $('#submit').click(function(){
        var name = $('#name').val();
        var age = $('#age').val();

        if( name== "" || age== ""){
          $('#response').fadeIn();
          $('#response').removeClass('success-msg').addClass('error-msg').html("All fields are required");
        }else{
          $.ajax({
          url: 'save-form.php',
          type: 'POST',
          data: $('#form-data').serialize(),
          success: function(result){
            $('#response').fadeIn();
            $('#response').removeClass('error-msg').addClass('success-msg').html(result);
          }
        });
    }
  });
});
</script>

</body>
</html>

`

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

probably your page is refreshed, try to use preventDefault to prevent the refresh

$('#submit').click(function(event){
        //your code here
      event.preventDefault();
    }
over 4 years ago · Santiago Trujillo Relatório

0

You have a button with type "submit".

<input type="submit" id="submit" value="Save">

As the click-event occurs on this button, your form will be send to the server. You have no action attribute defined on your form, so it redirects after submit to the same URL.

As Sterko stated, you could use a event-handler to prevent the submission of your form due to the submit button.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda