2012-03-08 80 views
1

我只是pluged一个JQuery username check后就正常存在的,问题是,我的形式仍然提交即使用户名存在于我的Mysq Database,我怎样才能将其配置为拒绝提交给我server side .php file如果存在?否认提交表单,如果用户名在MySQL数据库

这里是我的jQuery插件javascript代码

$(document).ready(function() { 


     //the min chars for checkusername 
     var min_chars = 3; 

     //result texts 
     var characters_error = 'Minimum amount of chars is 3'; 
     var checking_html = '<img src="images/checkusername.gif" /> Checking...'; 

     //when button is clicked 
     $('#check_checkusername_availability').click(function(){ 
      //run the character number check 
      if($('#checkusername').val().length < min_chars){ 
       //if it's bellow the minimum show characters_error text 
       $('#checkusername_availability_result').html(characters_error); 
      }else{   
       //else show the cheking_text and run the function to check 
       $('#checkusername_availability_result').html(checking_html); 
       check_availability(); 
      } 
     }); 


    }); 

//function to check checkusername availability 
function check_availability(){ 

     //get the checkusername 
     var checkusername = $('#checkusername').val(); 

     //use ajax to run the check 
     $.post("frontend/functions/f_checkuser.php", { checkusername: checkusername }, 
      function(result){  
       //if the result is 1 
       if(result == 1){ 
        //show that the checkusername is available 
        $('#checkusername_availability_result').html('<span class="is_available"><b>' +checkusername + '</b> is Available</span>'); 
       }else{ 
        //show that the checkusername is NOT available 
        $('#checkusername_availability_result').html('<span class="is_not_available"><b>' +checkusername + '</b> is not Available</span>'); 
       } 
     }); 

} 

这里是我的html场

<table border="0" > 
      <tr> 
      <td valign="middle"><input class="input_field_12em required userUserName" name="userUserName" id="checkusername"></td> 
      <td valign="middle"><input type='button' id='check_checkusername_availability' value='Check Availability'></td> 
      <td><div id='checkusername_availability_result'></div></td> 
      </tr> 
     </table> 
+0

什么形式?我看不到任何形式。那么你可以用js禁用它。 – 2012-03-08 21:29:50

+0

对不起,只是将html表单添加到问题 – 2012-03-08 21:32:21

+0

“frontend/functions/f_checkuser.php”返回什么? – ShankarSangoli 2012-03-08 21:32:46

回答

2

您可以等待回调解雇你的AJAX请求,那么您可以提交表单或不:

$(function() { 
    $('form').on('submit', function (event, extra) { 
     if (typeof extra == 'undefined') { 
      extra = false; 
     } 
     //if no extra argument is passed via `.trigger()` then don't submit the form 
     return extra; 
    }); 
    $('#check_checkusername_availability').on('click', function() { 
     $.ajax({ 
      url : 'frontend/functions/f_checkuser.php', 
      type : 'post', 
      data : { checkusername : $('#checkusername').val() }, 
      success : function (serverResponse) { 
       //now check the serverResponse variable to see if the name exists, if not then run this code: 
       $('form').trigger('submit', true); 
      }, 
      error : function (jqXHR, textStatus, errorThrown) { /*don't forget to handle possible errors*/ } 
     }); 
    }); 
}); 

否则,您可以强制AJAX请求通过setti同步但这会锁定浏览器,直到AJAX请求解决,这可能是用户无法做任何事的秒数。这给人的印象是你的脚本被用户打破了。

0

你没有发布你的html,但它似乎像点击对象的ID为check_checkusername_availability触发器形式提交以某种方式。它是一个提交按钮?如果是这种情况,只需将其更改为常规按钮即可。并添加回调来处理响应。

相关问题