2012-03-08 31 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觸發器形式提交以某種方式。它是一個提交按鈕?如果是這種情況,只需將其更改爲常規按鈕即可。並添加回調來處理響應。