How to send all form fields with $ .post?

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 a way to get the name and value of the inputs and do POST without typing each attribute and its value?

    
asked by anonymous 23.06.2014 / 05:36

1 answer

5

I believe the serialize() method 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());
    });

});
    
23.06.2014 / 06:01