0

我想在用户注册时检查用户名的可用性。我在前端工作。后端代码给了我。

这些是 signup.php 中的 php 代码

if (isset($_GET['chkusername']))
JSON_username_avail($_GET['chkusername']);

function JSON_username_avail($username) {
    $ret = array();
    print json_encode(validate_username($username, $ret));
    die();
}

function validate_username($username, & $retval_arr) {
    if ($username == NULL)
        $retval_arr['E_UserName'] = "NULL_USERNAME";
    else if (!username_validation($username))
        $retval_arr['E_UserName'] = "INVALID_USERNAME";
    else if (!data_not_exists("user", "username", $username, TRUE))
        $retval_arr['E_UserName'] = "USERNAME_EXISTS";
    return $retval_arr;
}

function username_validation($user) {
    $username = str_split($user);
    foreach($username as $i) {
        $i = ord($i);
        if ($i >= 48 and $i <= 57)
            continue;
        if ($i >= 65 and $i <= 90)
            continue;
        if ($i >= 97 and $i <= 122)
            continue;
        return FALSE;
    }
    return TRUE;
}


function data_not_exists($table, $field, $data, $CSense = FALSE) {
    $conn = connect_db();
    $data = filter_var($data, FILTER_SANITIZE_STRING);
    if ($CSense == TRUE)
        $sql = "SELECT * FROM ".$table.
    " WHERE ".$field.
    "='".$data.
    "'";
    else
        $sql = "SELECT * FROM ".$table.
    " WHERE upper(".$field.
    ")='".$data.
    "'";
    $result = mysqli_query($conn, $sql);

    switch ($result - > num_rows) {
    case 0:
        return TRUE;
        break;
    case 1:
        return FALSE;
        break;
    default:
        die("500 Internal Server Error: 122");

    } //switch

}

现在我不知道那么多的php。我创建了一个 javascript 函数来将用户名发送到 signup.php 页面进行验证。

这是我的功能

function submit_form() {
    var u = document.getElementById("username").value;
    $.post("signup.php", {
            "chkusername": u
        },
        function (data) {
            var x = data; //here i dont know how to get the return string. Whether it is NULL_USERNAME OR INVALID_USERNAME OR USERNAME_EXISTS.

        }, "json");
}

在这里,我将 x 的值作为 [object Object]。但我需要将返回消息存储在变量 x 中。我想知道它是 NULL_USERNAME OR INVALID_USERNAME OR USERNAME_EXISTS。请帮助我。

4

2 回答 2

2

尝试,

function(data){
  var x = data.E_UserName;

}, "json");

还根据MrCode 的回答将您的请求更改为GET

function submit_form() {
    var u = document.getElementById("username").value;
    $.get("signup.php", {
            "chkusername": u
        },
        function (data) {
            var x = data.E_UserName

        }, "json");
}
于 2013-06-14T13:05:48.543 回答
2

用户名已发布,但在 PHP 中您尝试使用 访问它$_GET,更改为:

if (isset($_POST['chkusername']))
JSON_username_avail($_POST['chkusername']);

此外,您的验证逻辑看起来不正确,如果用户名有效且可用怎么办?我会添加一个 else 子句并设置一个success变量:

function JSON_username_avail($username) {
    $ret = array();
    print json_encode(validate_username($username));
    die();
}

function validate_username($username) {
    $retval_arr = array('success' => false, 'message' => '');

    if ($username == NULL)
        $retval_arr['message'] = "NULL_USERNAME";
    else if (!username_validation($username))
        $retval_arr['message'] = "INVALID_USERNAME";
    else if (!data_not_exists("user", "username", $username, TRUE))
        $retval_arr['msessage'] = "USERNAME_EXISTS";
    else
        $retval_arr['success'] = true;
    return $retval_arr;
}

和阿贾克斯:

if(!data.success){
    console.log(data.message);
} else {
    // valid and available
}
于 2013-06-14T13:13:27.670 回答