send form without refreshing page

Asked

Viewed 1,363 times

2

Well I’m assembling the code in jquery that will send the form, it is working. However I have some questions and I do not know how to solve.

1º - To avoid that I have to do a very repetitive javascript I wanted to know how to put the link in the 'action' tag of the 'form' and make the jquery capture the Link and put it in the 'url: "fone2.php",'.

This would make me use the same code for all forms.

2º - I need to capture the message from the file that receives the form and put it in ' Alert("sdfsdf");'.

Can someone help me with these changes.

< script type = "text/javascript" >
  jQuery(document).ready(function() {
    jQuery('#ajax_form').submit(function() {
      var dados = jQuery(this).serialize();

      jQuery.ajax({
        type: "POST",
        url: "fone2.php",
        data: dados,
        success: function() {
          alert("sdfsdf");
        }
      });

      return false;
    });
  }); <
/script>
< script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script>

  <form method="post" action="" id="ajax_form">
    <input type="text" name="nome" value="" />
    <input type="submit" name="enviar" value="Enviar" />
  </form>

1 answer

3


I believe this will work out just the way you need it.

jQuery('document').ready(function() {
  jQuery('#ajax_form').submit(function() {
    var dados = jQuery(this).serialize();
    //aqui voce pega o conteudo do atributo action do form
    var url = $(this).attr('action');
    jQuery.ajax({
      type: "POST",
      url: url,
      data: dados,
      success: function(response) {
        //'response' é a resposta do servidor
        alert(response);
      }
    });

    return false;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="minha-url.php" id="ajax_form">
  <input type="text" name="nome" value="" />
  <input type="submit" name="enviar" value="Enviar" />
</form>

  • vlw was just that, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.