注册验证和ajax认证

时间:2013-01-24 12:42:20

标签: php jquery ajax forms jquery-mobile

我有一个注册表格,当填写并按下“注册”按钮被js检查,找到空字段并检查用户名的可用性,或者如果已经使用电子邮件或手机号码通过ajax发送信息到php并接受答案。但是我的js不会一直工作。这是我的js脚本:

$("#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));
}
?>

1 个答案:

答案 0 :(得分:1)

你的php文件中缺少一个括号:

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

应该是

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