动态复选框,从数据库中获取数据并相应地勾选复选框

时间:2014-07-03 08:25:25

标签: javascript php json checkbox

我想要做的是从数据库中获取数据并按照那个勾选复选框,但我没有成功任何想法?

我不确定是什么问题

JS代码

function editPer(role_pk) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState === 4) {
            var jsonObj = JSON.parse(xmlHttp.responseText);
            document.getElementById("role_pk1").value = jsonObj.pk;
            document.getElementById("role_name1").value = jsonObj.name;
            for (var i = 1; i < 29; i++){
                if (jsonObj.permission_fk+i != null)
                    document.getElementById("per"+i).checked = true;
                else 
                    document.getElementById("per"+i).checked = false;
            }

        }
    };

    xmlHttp.open("GET", "classes/controller/RoleController.php?action=getp&pk=" + role_pk, true);
    xmlHttp.send();

    /*
     * Reset error message
     * Show update role button
     */
    resetErrorMessage("roleMessage");
    showUpdateRoleButton();

    $("#perModalDetail").modal('show');
};

PHP代码RoleController.php

}else if ($action == "getp") {
    if (isset($pk)) {
        /*
         * Select single Role value
         */
        $itemArray = $roleDao->getp($pk);
        if (count($itemArray) == 0) {
            echo NULL;
        } else {
            echo json_encode($itemArray[0]);
        }
    }

类roleDao中的PHP代码

public function getp($pk) {
        $query = "SELECT pk, name FROM roles WHERE pk=".$pk.";";
        $result = mysql_query($query, $this->dbconn);

        $itemArray = array();
        while ($row = mysql_fetch_row($result)) {
            $item = array("pk" => $row[0], "name" => $row[1]);
            array_push($itemArray, $item);
        }

        $query = "SELECT permission_fk FROM role_permission WHERE role_fk=".$pk.";";
        $result = mysql_query($query, $this->dbconn);

        $i=1;
        while ($row = mysql_fetch_row($result)) {
            $item = array("permission_fk$i" => $row[0]);
            $i++;
            array_push($itemArray, $item);
        }
        //print $itemArray;
        return $itemArray;
    }

0 个答案:

没有答案