删除和确认中不允许使用方法消息未显示Cakephp 3

时间:2018-06-21 06:25:46

标签: cakephp

我一直在尝试一次删除多个用户的功能,但是它给了我错误消息 “方法不允许”,删除时也没有收到确认消息

控制器:

public function deleteselected()

{
$this->request->allowMethod(['user', 'delete']);
$data = $this->request->getData('ids');
foreach ($data as $value) {
    $this->Users->deleteAll(['id'=>$value]);
}
return $this->redirect(['action'=>'index']);
}

索引脚本:

<script>
$('.selectall').click(function(){
 $('.selectbox').prop('checked',$(this).prop('checked'));
$('.selectall2').prop('checked',$(this).prop('checked'));
});
$('.selectall2').click(function(){
$('.selectbox').prop('checked',$(this).prop('checked'));
$('.selectall').prop('checked',$(this).prop('checked'));
});
$('.selectbox').click(function(){
var total = $('.selectbox').length;
var number = $('.selectbox:checked').length;
if(total == number){
 $('.selectall').prop('checked', true);
$('.selectall2').prop('checked', true);
}else {
$('.selectall').prop('checked', false);
$('.selectall2').prop('checked', false);
 }});
</script>

索引按钮:

<form method="user">
 <p>
<button type="submit" formaction="<?php echo $this->Url- 
>build(['action'=>'deleteselected']) ?>" class="btn btn-danger">Delete 
Selected</button>
</p> 
   <tr>
        <th><input type="checkbox" class="selectall"/></th>
   </tr>
 <td><input type="checkbox" class="selectbox" name="ids[]" value="<?php 
 echo $user->id ?>" 
  /></td>

<button type="submit" formaction="<?php echo $this->Url->build(['action' 
=>'delete', $user->id]) ?>" class="btn btn-danger" onclick="return 
confirm('Are you sure?)">Delete</button>

1 个答案:

答案 0 :(得分:0)

显示确认消息和方法成功运行try this code


  

模板(index.ctp)

<?php

    $user_data = [
        '12',
        '13',
        '14',
        '15',
    ]
?>
<form method="post">
<p>
    <button type="submit" formaction="<?php echo $this->Url->build(['action'=>'deleteselected']) ?>" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete users?')">
        Delete Selected
    </button>
</p>
<table>
    <tr>
        <th><input type="checkbox" class="selectall"/></th>
        <th>User ID</th>
        <th>Action</th>
    </tr>
    <?php foreach ($user_data as $key => $id) { ?>
        <tr>
            <td>
                <input type="checkbox" class="selectbox" name="ids[]" value="<?= $id ?>"/>
            </td>
            <td><?= $id ?></td>
            <td>
                <button type="submit" formaction="<?php echo $this->Url->build(['action' =>'delete', $id]) ?>" class="btn btn-danger" onclick="return confirm('Are you sure?')">
                    Delete
                </button>
            </td>
        </tr>
    <?php } ?>
</table>
</form>
  

控制器

public function deleteselected()
{
   $this->request->allowMethod(['post', 'delete']);
   $data = $this->request->getData('ids');
   print_r($data);exit;
}