简体   繁体   English

CakePHP使用Ajax删除

[英]CakePHP delete using 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:- 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 call : 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>

please help me ... i want delete records without refresh page. 请帮助我...我想删除没有刷新页面的记录。

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更改为:在data添加id作为键,以便在控制器中访问它。更好地添加成功部分以确认操作消息。

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

            });

Let give you a solution step by step 让我们逐步为您提供解决方案

At first in your index.ctp 首先在您的index.ctp中

You can apply this code 您可以应用此代码

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>";
}

Then If you use jquery ajax get method you can use below code. 然后,如果您使用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();
     });
});

According to your controller code you can add this like below code 根据您的控制器代码,您可以像下面的代码一样添加

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'));
}

This code should work fine. 此代码应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM