在codeiginter中删除之前如何使用确认

时间:2018-10-31 09:58:31

标签: php codeigniter

$(document).ready(function (){
  $('#Delete').on('click', function() {

    var no = $('#bill').val();

    if(confirm('Are you sure to remove this record ?'))
    {
      $.ajax({
        url: "<?php echo base_url();?>TipUp_Loan/Bill_Delete"+no,
        type: 'DELETE',

        error: function() {
          alert('Something is wrong');
        },
        success: function(data) {
          //  $("#"+no).remove();
          alert("Record removed successfully");  
        }
      });
    }
  });
});

这是查看页面代码...

public function Bill_Delete(){
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];
    //$Search = $this->input->post('Search1');

    $this->User_model->Bill_Delete($_POST["no"]);
}

此控制器代码...........

public function Bill_Delete($no)
{
    $this->db->where('billno', $no);
    $this->db->delete('salesitem');
    $this->db->where('no', $no);
    $this->db->delete('salesbill');
    echo "Successfully delete";
}

这是模型代码

我的prblm是如何创建确认消息。在此仅显示该消息,而不是将其删除到数据库中..............

3 个答案:

答案 0 :(得分:1)

您可以通过添加此窗口来实现这一点。在按钮标记中进行确认

<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure want to delete')">Delete<i class="icon-bin position-left"></i></button>

答案 1 :(得分:0)

已修改:

问题是您没有在AJAX调用中发送变量'no'的值。使用Jquery AJAX的'data'属性发送变量。另外,对AJAX调用使用类型:“ post”。 这是修改后的代码:

$(document).ready(function (){
$('#Delete').on('click', function() {

var no = $('#bill').val();

if(confirm('Are you sure to remove this record ?'))
{
  $.ajax({
    url: "<?php echo base_url();?>TipUp_Loan/Bill_Delete",
    type: 'POST',
    data: { no: no },
    error: function() {
      alert('Something is wrong');
    },
    success: function(data) {
      alert("Record removed successfully");  
    }
  });
}
});
});

在控制器内部:

    public function Bill_Delete(){
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['username'];
    //$Search = $this->input->post('Search1');

    $this->User_model->Bill_Delete($_REQUEST["no"]);
    } 

其余的都适合您的模型和控制器。按照ThisGuyHasTwoThumbs的建议,认为混合JS和PHP的方式看起来很丑。

答案 2 :(得分:0)

我还有一个答案

<button type="submit" id="Delete" onclick="return confirm('Are You sure want to Delete')" class="btn btn-primary" >Delete<i class="icon-bin position-left"></i></button>

使用此可以确认框已启用