在zf2项目中集成ajax脚本

时间:2017-04-24 17:05:42

标签: javascript ajax zend-framework2

我想使用ajax保存复选框更改。但我无法获得任何已保存的更改。

这是视图:actionacces.phtml  

<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



$title = 'Action \'s acces by role ';
$this->headTitle($title);
?>

<h1><?php echo $this->escapeHtml($title); ?></h1>
<table class="table">
    <tr>
        <th>Action</th>
        <?php foreach ($roles as $role) : ?>
            <th><?php echo $this->escapeHtml($role['name']); ?>     </th>

        <?php endforeach; ?>
    </tr>
    <tr><?php foreach ($actions as $action) : ?>

            <th> <?php echo $this->escapeHtml($action['name']);
            '<\br>' ?></th>
            <?php
            foreach ($roles as $role) :
                $exist = 0;

                foreach ($acls as $acl) :

                    if ($acl['fk_role_id'] == $role['id'] && $acl['fk_action_id'] == $action['id'] && $acl['acces'] == 1) {
                        $exist = 1;
                    }

                endforeach;
                ?>
                <th>  <?php if ($exist == 1) { ?>
                        <label class="css-input switch switch-sm switch-primary push-10-t">
                            <input type='checkbox' class="checkboxAccess" id_role="<?= $role['id'] ?>" id_action="<?= $action['id'] ?>" acces="<?= $exist ?>" checked><span></span>
                        </label>   
                    <?php } else { ?>
                        <label class="css-input switch switch-sm switch-primary push-10-t">

                            <input type="checkbox" class="checkboxAccess" id_role="<?= $role['id'] ?>" id_action="<?= $action['id'] ?>" acces="<?= $exist ?>"  > <span></span>
                        </label>   
                        </th>
                <?php }
            endforeach;
            ?>

        </tr>

<?php endforeach; ?>

</table>

这是droit.js:

/ *      *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。      *要更改此模板文件,请选择“工具”|模板      *并在编辑器中打开模板。      * /

$(document).ready(function () {  
    $(".checkboxAccess").on('click', function () {
//        console.log($(this).attr("id_role"));   
//        var role_id = $(this).attr("id_role");
//          var action_id = $(this).attr("id_action");
//          var droit = $(this).is(':checked');
        console.log($(this).attr("id_role"));   
        var role_id = $(this).attr("id_role");
          var action_id = $(this).attr("id_action");
        var acces = $(this).is(':checked');

//      alert(role_id);
//      alert(action_id);

        $.ajax({
            type: "POST",
//                url:'Privilege/ACL/addACL',
            url: 'Detect/Acl/modifrole',
//            data: {
//                "id_role": role_id,
//                "id_action": action_id,
//                "droit": droit},
            data: {
                "id_role": role_id,
                "id_action": action_id,
                "acces": acces
                },

            Success: function (result) {
                alert('Success');
                console.log(result);
            },
            Error: function () {
                alert('Error');
            }})
    });

});  

控制器中的这个功能:

    public function modifroleAction() {

        $request = $this->getRequest();
//        echo'<pre>';        print_r($this->getRequest());die;
        if ($request->isPost()) {
            $parametres = $this->params()->fromPost();
            $acl = new Acl();
            $acl->fk_role_id = $parametres['role_id'];
            $acl->fk_action_id = $parametres['action_id'];
            $acces = $parametres['acces'];
            if ($acces == "false") {
                $acl->acces = 0;
            } else {
                $acl->acces = 1;
            }
            $this->getAclTable()->saveAcl($acl);

            return $this->redirect()->toRoute('acl');
        }
    }

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您试图通过$ parametres [&#39; role_id&#39;]和$ parametres [&#39; action_id&#39;]获取数据,这意味着role_id和action_id

但你发送 -

data: {
            "id_role": role_id,
            "id_action": action_id,
            "acces": acces
            },

表示id_role和id_action。

更改您的帖子索引可能有效。