Jquery Ajax POST Snippet

This is how to do with jquery ajax with post method snippet;

<div>
    <input type="text" id="one" name="one">
    <input type="text" id="two" name="two">
    <div id="btnSubmit">Submit</div>
</div>
<script>
    $("#btnSubmit").click(function(){
        var data = "one="+$("#one").val()+"&two="+$("#two").val();
       $.post("/url_to_save_data",data).done(function(){
           window.location.href="/url_to_redirect";
       });
    });
</script>

And the other one is:

<form id="nu_form">
    <input type="text" name="a_name" />
    <input type="text" name="something" />
 <input type="submit" value="save" id="btn_save" />
</form>
<script>
$("#btn_save").click(function(){ 
    $.ajax({ 
        data: $("#nu_form").serialize(), 
        type: 'POST',  
        url: '/save_data', 
        success: function(response) { 
            alert(response); } 
    }); 
    return false; 
});
</script>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s