将详细信息添加到数据库时发出警报错误

时间:2018-10-25 14:29:51

标签: javascript

尽管成功创建了用户,但我收到错误消息“无法创建用户”。我已评论了显示eeror的部分。所有细节都可以存储到数据库中。当我将其更改为alert(arr [0] .result)时,输出为0。我尝试使用inspect检查是否有任何错误,但没有任何错误。

(function () {

    var admin_userid; 
    var admin_password;               
    var admin_passwordagain; 
    var admin_firstname;            
    var admin_lastname;
    var admin_position;   


    $(document).ready(function () {

        $("#NewAdminForm").validate({
                rules: {
        txtNewLogin: "required",
        txtNewPassword: "required",
        txtNewPasswordAgain: {
            equalTo: "#txtNewPassword"
        },
        txtFirstName: "required",
        txtLastName: "required",
        txtNewPosition: "required"
    },
    messages: {
        txtNewLogin: "new user name is required",
        txtNewPassword: "new password is required",
        txtNewPasswordAgain: "new password again is required and must be the same as new password",
        txtFirstName: "First Name is required",
        txtLastName: "Last Name is required",
        txtNewPosition: "Position is required"
    },
    focusInvalid: false,
    submitHandler: function () {
        return false;
    },
    errorPlacement: function (error, element) {
        error.appendTo(element.parent().parent().after());
    },
        });


        $("#btnCreateAccount").bind("click", function () {
            savenewuser();
        });
    });

    function savenewuser() {
        if ($("#NewAdminForm").valid()) { 

        admin_userid = $("#txtNewLogin").val(); 
        admin_password = $("#txtNewPassword").val();  
        admin_passwordagain = $("#txtNewPasswordAgain").val();  
        admin_firstname = $("#txtFirstName").val();  
        admin_lastname = $("#txtLastName").val();
        admin_position = $("#txtNewPosition").val();

        if ($("#NewAdminForm").valid()) {
                var url = serverURL() + "/newadmin.php"; 

                var JSONObject = {
                    "admin_userid": admin_userid,
                    "admin_password": admin_password,
                    "admin_firstname": admin_firstname,
                    "admin_lastname": admin_lastname,
                    "admin_position": admin_position
                };

                $.ajax({
                    url: url,
                    type: 'GET',
                    data: JSONObject,
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    success: function (arr) {
                        _getAdminResult(arr);
                    },
                    error: function () {
                        alert("error");
                    }
                });
            }
        }
    }

    function _getAdminResult(arr) {
        if (arr[0].result == 1) {
            localStorage.setItem("admin_userid", admin_userid); 
            localStorage.setItem("admin_password", admin_password);
            alert("new user created");
            window.location = "homepage_admin.php"; 
        }
        else {
            alert("User is unable to be created"); //error displayed
        }
    }
})();

0 个答案:

没有答案