2013-01-24 112 views
2

我有一個註冊表格,當填寫完畢並且按下「註冊」按鈕時,js會檢查它,找到空字段並檢查用戶名的可用性,或者電子郵件或移動號碼已被通過ajax發送信息到php並接收答案。但我的js不會一直工作。這是我的JS腳本:ajax註冊驗證和認證

$("#reg_button").click(function(){ 
    user = $("#usr").val(); 
    pass = $("#psw").val(); 
    fname = $("#first_name").val(); 
    sname = $("#second_name").val(); 
    dateb = $("#date_birth").val(); 
    email = $("#email").val(); 
    mobnum = $("#mob_num").val(); 

    if(user == ""){ 
     alert("First name must be filled out"); 
     $('#usr').focus(); 
     return false; 
    }else if(pass == ""){ 
     alert("Password must be filled out"); 
     $('#psw').focus(); 
     return false; 
    }else if(fname == ""){ 
     alert("First name must be filled out"); 
     $('#first_name').focus(); 
     return false; 
    }else if(sname == ""){ 
     alert("Second name must be filled out"); 
     $('#second_name').focus(); 
     return false; 
    }else if(dateb == ""){ 
     alert("Date of birth must be filled out"); 
     $('#date_birth').focus(); 
     return false; 
    }else if(email == ""){ 
     alert("Email must be filled out"); 
     $('#email').focus(); 
     return false; 
    }else if(mobnum == ""){ 
     alert("Mobile number must be filled out"); 
     $('#mob_num').focus(); 
     return false; 
    }else{ 
     ajaxCheck(); 
    } 

    function ajaxCheck(){ 
     $.ajax({ 
      type: "POST", 
      url: "http://imes.*********.com/php/check_info_reg.php", 
      data: "usr="+user+"&email="+email+"&mob_num="+mobnum, 
      dataType: "json", 
      success: function(json){ 
       if(json.success){ 
        var user_conf = json.data.usr; 
        var email_conf = json.data.email; 
        var mob_conf = json.data.mob; 

        if(user_conf == "taken"){ 
         alert("Username already taken. Choose another one."); 
         $('#usr').focus(); 
         return false; 
        } 

        if(email_conf == "taken"){ 
         alert("Email already registered. If you lost your password, retrieve it on login page."); 
         $('#email').focus(); 
         return false; 
        } 

        if(mob_conf == "taken"){ 
         alert("Mobile number already registered. If you lost your password, retrieve it on login page."); 
         $('#mob_num').focus(); 
         return false; 
        } 
       }else{ 
        //Here could go another ajax, for actualy sending the 
        //info into the php script which sends it to database. 
       } 
      }, 
      beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner 
      complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner 
     }); 
     return false; 
    } 
}); 

而且我的PHP:

<?php 
$username = mysql_real_escape_string($_POST['usr']); 
$email = mysql_real_escape_string($_POST['email']); 
$mob_num = mysql_real_escape_string($_POST['mob_num']); 

include('mysql_connection.php'); 
mysql_select_db("jzperson_imesUsers", $con); 

$sql1 = mysql_query("SELECT * FROM registered_user WHERE username='$username'"); 
$sql2 = mysql_query("SELECT * FROM registered_user WHERE email='$email'"); 
$sql3 = mysql_query("SELECT * FROM registered_user WHERE mobile_num='$mob_num'"); 

$res1 = mysql_num_rows($sql1); 
$res2 = mysql_num_rows($sql2); 
$res3 = mysql_num_rows($sql3); 

if(isset($username) && !empty($username){ 
if($res1 >= 1){ 
    //JSON message: Username already taken. Choose different. 
    echo json_encode(array('success'=>true, 'usr'=>"taken")); 
} 
} 

elseif(isset($email) && !empty($email)){ 
if($res2 >= 1){ 
    //JSON message: Email already registered. Retrieve on "login"(login => link). 
    echo json_encode(array('success'=>true, 'email'=>"taken"));; 
} 
} 

elseif(isset($mob_num) && !empty($mob_num)){ 
if($res3 >= 1){ 
    //JSON message: Mobile number already registered. Retrieve on "login"(login => link). 
    echo json_encode(array('success'=>true, 'mob'=>"taken")); 
} 
} 

else{ 
echo json_encode(array('success'=>false)); 
} 
?> 
+2

[ **請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql) 。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –

+0

如果您也會發布錯誤訊息,那麼這很容易,因此我們不必通過您的問題(很長) – bipen

+0

沒有得到任何TBH。但問題是,在輸入相同的用戶名,電子郵件或電話號碼後,腳本只會通過,它不會提醒我,只會在未填寫的字段中提醒。所以Ajax部分必須有一些問題。 –

回答

1

你缺少你的PHP文件中的括號:

if(isset($username) && !empty($username){ 

應該

if(isset($username) && !empty($username)){ 
+0

謝謝,有用但可惜沒有解決我的問題。 –

+0

你是否曾經完成這項工作,如果你有這個問題,你可以發佈你的工作解決方案嗎? – BarclayVision