2017-04-24 57 views
0

我想用ajax保存复选框更改。但我无法获取任何保存的更改。在zf2项目中集成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'); 
     } 
    } 

你能帮助我吗?

+0

你检查了你的ajax.url:'Detect/Acl/modifrole',?或者你在控制器modifroleAction()中获取数据? –

回答

0

您正试图通过$获取数据参数应用[ 'ROLE_ID']和$参数应用[ '的action_id'],这意味着ROLE_ID和的action_id

但是你发送 -

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

这意味着id_role,和id_action。

更改您的帖子索引可能工作。