游.程 2015-11-05 05:37 采纳率: 0%
浏览 16

CakePHP使用Ajax删除

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 code:-

 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 call :

 <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>

please help me ... i want delete records without refresh page.

  • 写回答

2条回答 默认 最新

  • ℙℕℤℝ 2015-11-05 07:03
    关注

    change ajax to this:Add id as key in data to access it in your controller.better add success part for confirmation msg of action.

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

报告相同问题?