Nathan
Nathan

Reputation: 509

ajax submit after html validation

im having a few problems with an ajax login form when i am clicking submit button its not validating the form before submitting the ajax its just submitting the form where am i going wrong

this is the ajax script

    function wa_login() {
        var wa_username = $('#wa_username').val();
        var wa_password = $('#wa_password').val();
        var datas = 'wa_username=' + wa_username + '&wa_password=' + wa_password;

        $.ajax({
            type: 'POST',
            url: '/limitless/functions.php',
            data: datas
        })
        .done(function (data) {
            $('#info').html(data);
        });
    }

here is the form itself

<form class='form-horizontal form-validate-jquery' action='#' novalidate='novalidate'>
<div class='text-center'>
<div class='icon-object border-slate-300 text-slate-300'><i class='icon-reading'></i></div>
<h5 class='content-group'>Login to your account <small class='display-block'>Enter your credentials below</small></h5>
    </div>

<div class='form-group has-feedback has-feedback-left'>
<input type='text' class='form-control' placeholder='Username' id='wa_username' required='required'>
 <div class='form-control-feedback'>
            <i class='icon-user text-muted'></i>
        </div>
    </div>

    <div class='form-group has-feedback has-feedback-left'>
        <input type='password' class='form-control' placeholder='Password' id='wa_password' required='required'>
        <div class='form-control-feedback'>
            <i class='icon-lock2 text-muted'></i>
        </div>
    </div>

    <div class='form-group'>
        <button type='submit' onclick='wa_login()'   class='btn btn-primary btn-block'>Sign in <i class='icon-circle-right2 position-right'></i></button>
    </div>

    <div class='text-center'>
        <a href='reset'>Forgot password?</a> | <a href='register' >Sign Up</a>
    </div>
    </form>

Upvotes: 0

Views: 1004

Answers (2)

wilsotobianco
wilsotobianco

Reputation: 1558

What you need to do is catch the form submit event with jQuery as follows:

$('#form').on('submit',function(event){
  
  event.preventDefault(); // prevent default behavior
  
  if(formIsValid()){  // Execute validations in formIsValid() function
                      // returning true if valid, false if not
    $.ajax({
            type: 'POST',
            url: '/limitless/functions.php',
            data: $(this).serialize() // automatically serializing form
        })
        .done(function (data) {
            $('#info').html(data);
    });
  } else{
    console.log('Sorry, wrong login credentials');
  }
  
  function formIsValid(){
    // Always returning false to show how submit is not called
    return false;
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Notice that an id is added to your form -->
<form id="form" class='form-horizontal form-validate-jquery' action='#' novalidate='novalidate'>
<div class='text-center'>
<div class='icon-object border-slate-300 text-slate-300'><i class='icon-reading'></i></div>
<h5 class='content-group'>Login to your account <small class='display-block'>Enter your credentials below</small></h5>
    </div>

<div class='form-group has-feedback has-feedback-left'>
<input type='text' class='form-control' placeholder='Username' id='wa_username' required='required'>
 <div class='form-control-feedback'>
            <i class='icon-user text-muted'></i>
        </div>
    </div>

    <div class='form-group has-feedback has-feedback-left'>
        <input type='password' class='form-control' placeholder='Password' id='wa_password' required='required'>
        <div class='form-control-feedback'>
            <i class='icon-lock2 text-muted'></i>
        </div>
    </div>

    <div class='form-group'>
        <button type='submit' class='btn btn-primary btn-block'>Sign in <i class='icon-circle-right2 position-right'></i></button>
    </div>

    <div class='text-center'>
        <a href='reset'>Forgot password?</a> | <a href='register' >Sign Up</a>
    </div>
</form>

Watch the console log, you must see a message alerting about wrong login credentials. Hope it helps!

Upvotes: 2

Marcus Abrah&#227;o
Marcus Abrah&#227;o

Reputation: 696

Change type button from submit to button then:

    function wa_login() {
        var wa_username = $('#wa_username').val();
        var wa_password = $('#wa_password').val();
        var datas = 'wa_username=' + wa_username + '&wa_password=' + wa_password;

        $.ajax({
            type: 'POST',
            url: '/limitless/functions.php',
            data: datas
        })
        .done(function (data) {
            $('#info').html(data);
            $("form").submit();
        });
    }

Upvotes: 1

Related Questions