CakePHP使用ajax删除

时间:2015-11-05 05:37:51

标签: php jquery ajax cakephp

in index.ctp my code is as follows :-
<?php
            foreach( $teacher as $teachers )
            {
                echo "<tr>";
                echo "<td>".$teachers['Teacher']['username']."</td>";
                echo "<td>".$teachers['Teacher']['dgen']."</td>";
                echo "<td>".$teachers['Teacher']['dqual']."</td>";                  

                <?php
                echo "<td>".$this->Form->postLink(__('Delete'), array('action' => 'delete',$teachers['Teacher']['id']), null, __('Are you sure you want to delete %s?', $teachers['Teacher']['id']))."</td>";

                echo "</tr>";
            }
        ?>      

mycontroller代码: -

 public function delete() 
    {
        $this->autoRender = false;
        $this->layout = 'ajax';
        $id = $_GET['id'];          
        $this->loadModel('Teacher');
        $this->Teacher->delete($id);
        //$this->Session->setFlash('The post with id: '.$id.' has been deleted.');
        //$this->redirect(array('action'=>'index'));
    }

Ajax电话:

 <script type="text/javascript">
        $('a.deleteajax').click(function(event){
        event.preventDefault();
        var answer = confirm("Delete this record?")
        if (answer){
            $.ajax({
                url:'/Teachers/TeachersController/delete/',
                type:'GET',
                data: $(this).attr("href")
            });
        }
        return false;  
        });
    </script>

请帮助我...我想删除没有刷新页面的记录。

2 个答案:

答案 0 :(得分:0)

将ajax更改为:在id中添加data作为密钥以在控制器中访问它。更新成功部分以确认操作消息。

$.ajax({
             url:'/Teachers/TeachersController/delete/',
                type:'GET',
                data: {id:$(this).attr("href")},
                success:function(resp){//reso is msg string returned from controller.
                    alert(resp);
                  }

            });

答案 1 :(得分:0)

让我们一步一步解决问题

首先在index.ctp中

您可以应用此代码

foreach( $teacher as $teachers )
{
          echo "<tr id='echo $teachers['Teacher']['id'];'>";  //used id for daynamic tr, for hide temporary your delete data.

          echo "<td>".$teachers['Teacher']['username']."</td>";
          echo "<td>".$teachers['Teacher']['dgen']."</td>";
          echo "<td>".$teachers['Teacher']['dqual']."</td>";                  
          echo $this->html->link('','#',
          array(
               'class'=>'del btn btn-sm btn-danger glyphicon glyphicon-remove',
               'id'=>$teachers['Teacher']['id']  //important 
          )); 

          echo "</tr>";
}

然后如果你使用jquery ajax get方法,你可以使用下面的代码。

$('.del').click(function(){
                    var x=$(this).attr("id");
                    jQuery.get("<?php echo  Router::url(array('controller'=>'teachers','action'=>'delete'));?>",               {"id":x},
   function(data,stat){

                    jQuery('#'+x).remove();
     });
});

根据您的控制器代码,您可以添加如下代码

public function delete($id=NUll) {
    $id=$_GET['id'];     // it's not a good practices you can use $this->request->data/param I mean cakephp syntax.  
    $this->Teacher->id = $id;  
    if ($this->Teacher->delete()) {
        $this->Session->setFlash(__('The user has been deleted.'));
    }
    return $this->redirect(array('action' => 'index'));
}

此代码应该可以正常工作。