警报错误,但详细信息保存在数据库中

时间:2018-10-25 06:09:31

标签: javascript

当我按提交时,它将显示“无法保存用户”错误。但是,所有详细信息都可以保存到数据库中。不确定出了什么问题,但由于它已保存到数据库中,因此应该显示“用户已保存”并引导用户到 homepage_admin.php 。我试图检查是否有错误,但是什么也没有。

$().ready(function() {
    "use strict";

    var userid;                   
    var password;          
    var passwordagain;    
    var firstname;
    var lastname;
    var position;


    $("#NewAdminForm").validate({
        rules: {
            txtNewLogin: "required",
            txtNewPassword: "required",
            //the user enters the same value for username and password
            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 () {
        if ($("#NewAdminForm").valid())
        savenewuser();
    });
});

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

    userid = $("#txtNewLogin").val();  
    password = $("#txtNewPassword").val();              
    firstname = $("#txtFirstName").val();
    lastname = $("#txtLastName").val();
    position = $("#txtNewPosition").val();

    var url = serverURL() + "/newadmin.php";
    var JSONObject = {
        "userid": userid,
        "password": password,
        "firstname": firstname,
        "lastname": lastname,
        "position": position
    };

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

function _getNewUserResult(arr) {
    if (arr[0].result === 1) { //newuser.php accepts this newuser 
        localStorage.setItem("userid", userid);
        localStorage.setItem("password", password);
        localStorage.setItem("firstname", firstname);
        localStorage.setItem("lastname", lastname);
        localStorage.setItem("position", position);
        alert("user saved");
        window.location = "homepage_admin.php";
    }
    else {
        alert("Cannot save user");
    }
}

添加了html

        <form class="NewAdminForm" id="NewAdminForm" action="">
        <fieldset>
            <div>
                <label for="userid">User ID</label>
                <input type="text" name="txtNewLogin" id="txtNewLogin" placeholder="enter user ID" value="peter" required><br/>
            </div>

            <div><label for="password">Password</label>
                 <input type="text" name="txtNewPassword" id="txtNewPassword" placeholder="enter password" value="peter" required><br/></div>

            <div>
                <label for="txtNewPasswordAgain">Re-enter password</label>
                <input type="text" name="txtNewPasswordAgain" id="txtNewPasswordAgain" placeholder="enter password again" value="peter" required><br/>
            </div>

            <div><label for="employeename">Name</label>
                 <input type="text" name="txtNewName" id="txtNewName" placeholder="enter name" value="peter parker" required><br/></div>

            <div><label for="role">Position</label>
                 <input type="text" name="txtNewRole" id="txtNewRole" placeholder="enter position" value="spider man" required><br/></div>

            <br />

            <div><input type="submit" id="btnCreateAccount" value="Create Account"></div>

            <br/>
        </fieldset>
        </form>

2 个答案:

答案 0 :(得分:0)

尝试检查传递给arr函数的_getNewUserResult的值,结果集可能与您期望的结果不同。

答案 1 :(得分:0)

  1. 首先通过以下方法检查arr [0] .result是否确实返回1: 成功完成ajax请求alert(arr[0].result);或_getNewUserResult函数后。

  2. 如果显示1,则代替arr [0]。结果=== 1 做 arr[0].result ==='1'或尝试arr[0].result ==1