繁体   English   中英

删除按钮在CodeIgniter中不起作用

[英]Delete button not working in CodeIgniter

这是我的删除按钮:

<td>
    <a href="javascript:void(0);" onclick="delete(<?php echo $data->emp_id;?>);">
        <button id="button_delete">Delete</button>
    </a>
</td>

这是我的Javascript代码:

<script type="text/javascript">
    var url="<?php echo base_url();?>";
    function delete(id)
    {
        var r=confirm("Do you want to delete this employee?");
        if (r==true)
        {
            window.location = url+"index.php/admin_logins/employee4/"+id;
        }
        else
        {
            return false;
        }
    }
</script>

这是我的控制器( admin_logins.php )方法-

function employee4($emp_id)
{
    $this->employee_model->delete_employee($emp_id);
    $this->index();
}

这是我的model( employee_model.php )方法-

public function delete_employee($emp_id)
{
    $this->db->where('employee.emp_id',$emp_id);
    return $this->db->delete('employee');
}

我的表名是employee ,我的数据库是MySQL。

Column Name         Datatype
   id                 int
  emp_id             varchar(15)
  emp_name           varchar(50)
  emp_mobile_no      varchar(15)

每当我单击“ 删除”按钮时,都不会发生任何事情。 我究竟做错了什么?

无需传递$emp_id 您的控制器功能应如下所示:

function employee4()
{
    $emp_id = $this->uri->segment(3);

    $this->employee_model->delete_employee($emp_id);
    $this->index();
}

在您的情况下,uri细分值为3

这是我测试过的函数名称delete ,并给了我这个错误:

未捕获的SyntaxError:意外删除令牌

因此,请尝试使用其他名称命名它,例如: deleteEntrydeleteEmployee

delete是javascript命令,因此是保留字,请始终避免使用保留字命名函数或变量

另外,我建议使用解决方案:

<td>
    <a href="index.php/admin_logins/employee4/<?php echo $data->emp_id;?>" 
       onclick="return confirm('Do you want to delete this employee?');">
        <button id="button_delete">Delete</button>
    </a>
</td>

a标签HREF,和刚才的onclick返回确认信息

面对与删除按钮相同的问题,最后我弄清楚了这些代码,它完全按照我想要的方式工作。

// *****我的控制器名称:Main *****

public function delete_user()
{
    $this->model_users->deleteM($id);
 // whatever you like here.. redirect('') OR load view;
  }

// ***** Model *****

  public function deleteM($data)
  {
     $this->db->where('id', $this->uri->segment(3));
     $this->db->delete('employee');
  }

// ***** VIEW ******* 

<?php $bttn = '<button name="delete" onclick="ConfirmDelete()"></button>'; 
echo anchor("main/delete_user/$row->id", $bttn);?> 

如果您需要有关此VIEW的帮助,您可以随时问,现在,我认为一切都很棒!

// ***** OnClick JS Function ******

<script>
  function ConfirmDelete()
  {
    var x = confirm("Are you sure you want to delete?");
    if (x)
      return true;
    else
      return false;
  }
</script>

尝试这个

$('#id_for_your_delete_button').on('click', function(){
     var id = $('#entry_id').val();
     $.ajax({
          type: 'POST',
          url: '<?= site_url("your_controller_here"); ?>',
          data: id
    });
});

暂无
暂无

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

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