I'm trying to validate form elements before sending it using AJAX request. It checks for error, but form gets submitted.
My code:
$('#accountFormAddEdit').on('submit', function(e){
e.preventDefault();
if($('#account').val() == "")
{
alert("Account name is required");
} else {
$.ajax({
url: '<?= base_url() ?>controller/method',
method: 'POST',
data: $('#accountFormAddEdit').serialize(),
beforeSend:function(){
$('#submit').val("Saving");
$('#accountForm').modal('hide');
},
success:function(data){
$('#accountFormAddEdit')[0].reset();
$('#accountForm').modal('hide');
},
dataType: 'JSON',
});
}
location.reload();
});
I'm using a HTML Modal
to show form And PHP Codeigniter
.
When I submit the form location.reload()
won't refresh the page, to get the result back I use to refresh the page.
So how to check for white space and prevent the form data submission and get the response without refresh?