Why does not this JavaScript code work?

1

Why does not this JavaScript work?

JavaScript code:

$('#basic_validate').submit(function(e) {
    document.getElementById('successmsg').style.display = 'none';
    document.getElementById('errormsg').style.display = 'none';
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../suppliers/resources/controllers/register.php",
        async: true,
        data: $(this).serialize(),
        success: function(data) { 
        },
        complete: function(result){
            if(result == true){
                //clear inputs
                document.getElementById('successmsg').style.display = 'block';
            }else{
                document.getElementById('errormsg').style.display = 'block';
            }
        }
    });
});

HTML code:

<form class="form-horizontal" method="post" name="frmLogin" id="frmLogin">
//text_inputs
    
asked by anonymous 29.05.2017 / 23:21

1 answer

1

If you are submitting by ajax you have to stop submitting by HTML. For this you have to use .preventDefault() .

Use this way:

$('#frmLogin').submit(function(e) {
    e.preventDefault(); // <------------
    $.ajax({
        type: "POST",
        url: "../controllers/insert.php",
        async: true,
        data: $(this).serialize(),
        success: function(data) { 
        },
        complete: function(result){
            if(result == 10)
                //clear inputs
                //show msg success
            else
                //show msg error
        }
    });
}); 
    
29.05.2017 / 23:34