csdn产品小助手
2019-11-15 02:24
采纳率: 0%
浏览 96

通过ajax php删除行

Hello I am just learning codeigniter, here I am displaying a database and there are several rows. I made a delete function with ajax, it worked, but it had to be reloaded, how so that when I click delete, the data is deleted and it doesn't have to be refreshed.

      <tbody id="tbody">
           <?php
              $no = 1;
                  foreach ($temporary as $m) { ?>
                      <tr>
                        <input type="hidden"  class="form-control" name="id_service" value="<?php echo $m->id_service ?>">
                        <input type="hidden"  class="form-control" name="id_cs" value="<?php echo $m->id_cs ?>">
                        <input type="hidden"  class="form-control" name="jenis" value="<?php echo $m->jenis ?>">
                        <td>
                          <input type="hidden"  class="form-control" name="id_tmp" value="<?php echo $m->id_tmp ?>">
                          <input type="text"  class="form-control" name="" value="<?php echo $m->tracking_number ?>">
                        </td>
                         <td>
                          <button type="button" class="btn btn-danger" onclick="deletes(<?php echo $m->id_tmp;?>)">Delete</button>
                        </td>
                      </tr>
               <?php } ?>
        </tbody>

ajax

function deletes(id){
if (confirm("Are you sure?")) {
    $.ajax({
       url: '<?php echo base_url();?>backend/inbound/del',
        type: 'post',
        data: {id_tmp:id},
        success: function () {
            alert('ok');
        },
        error: function () {
            alert('gagal');
        }
    });
} else {
    alert(id + " not deleted");
}
}

图片转代码服务由CSDN问答提供 功能建议

                    

你好,我只是在学习codeigniter,在这里我显示一个数据库,有几行。 我用ajax做了一个删除功能,它可以工作,但是必须重新加载它,如何在我单击删除时删除数据,而不必刷新。

  
            <?php
               $ no = 1;
                   foreach($ temporary as $ m){?>
                       
                          id_service?>”>
                          id_cs?>”>
                          jenis?>”>
                         
                            id_tmp?>”>
                            tracking_number?>”>
                         
                          
                           
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 10.24 2019-11-15 02:32

    Since you've mentioned it's deleting on the backend or your database, you could just use jquery to delete that row on the UI. Here's one way without modifying your markup.

    1. First is you add a class to the input field that contains the ID. I used id-input
    <input type="hidden"  class="form-control id-input" name="id_tmp" value="<?php echo $m->id_tmp ?>">
    
    1. Then use this ajax to navigate through the input fields with that class and value to delete the row. See my code on success function;
    function deletes(id){
    if (confirm("Are you sure?")) {
        $.ajax({
           url: '<?php echo base_url();?>backend/inbound/del',
            type: 'post',
            data: {id_tmp:id},
            success: function () {
    
                // loop through all input with class id-input
                $(".id-input").each(function(){
    
                   // if it matches the value delete parent row
                   if($(this).val() == id){
    
                      // delete parent row
                      $(this).parent().parent().remove();
                   }
                });
            },
            error: function () {
                alert('gagal');
            }
        });
    } else {
        alert(id + " not deleted");
    }
    }
    

    EDIT: use .parent().parent() as we need to refer to tr, not td

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题