How to submit all form fields with $.post?

Asked

Viewed 419 times

3

I have a form with 2 fields, User and Password.

$.post('recebe.php',{
    `Nome:'Meu Nome'`
    `Senha:'Minha Senha'`
},function(data){
    alert(data);
});

Is there any way to take the name and value of inputs and do POST without having to type each attribute and its respective value?

  • You want it to automatically pick up the fields in a form and use your names and values as parameters for the post. Is that it? Or did I get it wrong?

1 answer

5


I believe the method serialize() solves the problem:

<form id="cadastro">
    <input type="text" name="nome"/>
    <input type="password" name="senha" />
    <button id="btn"type="button">teste</button>
</form>

$(document).ready(function(){
    $(' #btn').click(function(){
        $.post( "test.php", $( "#cadastro" ).serialize())
          .done(function( data ) {
            alert( "Data Loaded: " + data );
          });
        alert($('#cadastro').serialize());
    });

});
  • 1

    Damn, man, you saved my life hehe, thank you so much :D

Browser other questions tagged

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