Muhammad Cahya
Muhammad Cahya

Reputation: 288

How to write if else statement AJAX POST JQuery

Im trying to use if else statement in success ajax function, but the value always false and data inserted
I use jQuery v1.7.1, here my code

// Contact Submiting form
function Contact_form(form, options){       
    $.ajax({
        url: 'contact.php',
        data: $('#contactform').serialize(),
        success: function(data){    
              if(data.check == '1'){ // if Ajax respone data.check =1 or Complete
                 $('.notifications').slideDown(function(){
                            setTimeout("$('.notifications').slideUp();",7000);                              
                        }); // Show  notifications box
                 $('#contactform').get(0).reset();  //  reset form

              }
              else if(data.check == '0'){ 
                  $('#preloader').fadeOut(400,function(){ $(this).remove(); });     
                  alert("No complete");
                  return false;
              }

        },
        cache: false,type: 'POST',dataType: 'json'
    });
}

Upvotes: 0

Views: 3295

Answers (2)

Bujangan Muda
Bujangan Muda

Reputation: 88

if else is bad in success function use

function Contact_form(form, options){       
    $.ajax({
        url: 'contact.php',
        data: $('#contactform').serialize(),
        success: function(data){
                 $('.notifications').slideDown(function(){
                        setTimeout("$('.notifications').slideUp();",7000);                              
                    }); // Show  notifications box
                 $('#contactform').get(0).reset();  //  reset form
                    unloading();     //  remove loading
        },
        error: function(data){  
              $('#preloader').fadeOut(400,function(){ $(this).remove(); });     
                            alert("No complete");
                return false;
        },
        cache: false,type: 'POST',dataType: 'json'
    });
}

Upvotes: 1

Feather_code
Feather_code

Reputation: 23

Can you try to use data.result in logic statement

Upvotes: 1

Related Questions