Kamis, 05 Juni 2014

Snippet for JQuery Ajax Post Method

This is how to do jquery ajax with post method;


Method 01;


<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>


Method 02;

<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>