2

我有一个注册表单,当填写并按下“注册”按钮时,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));
}
?>
4

1 回答 1

1

您的 php 文件中缺少括号:

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

应该

if(isset($username) && !empty($username)){
于 2013-01-24T12:53:08.427 回答