0

I'm new to Jquery ajax, and I'm trying to submit certain field only for checking instead of submitting the whole form. Here I have made a function for checking the username whether is it available, and it works fine, but I doesn't want it to submit the whole form when doing the checking.

Here is my script:

<form id="userCheck">
<input type="text" class="register_field" name="fr_username" id="fr_username" />
<input type="text" class="register_field" name="fr_password" id="fr_password" />
<input type="text" class="register_field" name="fr_password1" id="fr_password1" />
</form>

<script>
$.validator.addMethod("duplicateCheck", function(value, element) {
            $.ajax({
        type: 'POST',
        url: '../a/checkDuplicate',
        data: $("#userCheck").serialize(),
        dataType: 'json'
    }).success(function(data){
        if(data.status == '1'){
            console.log('Available!');
        }
        else {
            console.log('not available');
            $('#fr_dupChkU_msg').html('');
        }
    });
        });
</script>
MuthaFury
  • 805
  • 1
  • 7
  • 22

1 Answers1

1

In short, that is how you get a value of an input field with jQuery:

<script type="text/javascript">
  var username = $('#fr_username').val();

  $.validator.addMethod('duplicateCheck', function(value, element) {
            $.ajax({
        type: 'POST',
        url: '../a/checkDuplicate',
        data: {username: username},
    }).success(function(data){
        if(data.status == '1'){
            console.log('Available!');
        }
        else {
            console.log('not available');
            $('#fr_dupChkU_msg').html('');
        }
    });
        });
</script>
Jurik
  • 3,244
  • 1
  • 31
  • 52