【问题标题】:Delete button not working in CodeIgniter删除按钮在 CodeIgniter 中不起作用
【发布时间】:2023-03-13 09:18:01
【问题描述】:

这是我的删除按钮:

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

这是我的模型(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)

每当我点击删除按钮时,什么都没有发生。我做错了什么?

【问题讨论】:

  • 您确定删除功能包含在正确的位置吗?我的意思是如果你执行的页面真的可以访问它,无论如何你可以检查控制台是否有任何错误
  • 我正在使用 Notepad++ 进行编码。如何检查控制台?有什么想法吗?
  • 当您在浏览器中执行页面时,您说按钮单击不起作用,因此请尝试使用开发人员工具查看任何 javascript 错误
  • 网页浏览器中的Ctrl+Shift+J
  • Ctrl+Shift+J。它没有告诉我任何关于我的 Javascript 中的删除功能的信息。

标签: javascript php mysql codeigniter


【解决方案1】:

试试这个

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

【讨论】:

  • 但这不是真正的问题,它在 JS 中
  • 这里直接使用 SQL 代替 Active 记录。为什么你认为这应该可以解决问题?
  • 不。什么都没有发生。
  • 除了使用 ajax 之外,还有其他方法可以做吗?
【解决方案2】:

是函数名delete我测试了一下,给我这个错误:

Uncaught SyntaxError: Unexpected token delete

所以尝试用另一个名字命名它,例如: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只返回确认信息

【讨论】:

  • 好的,我把函数名改成了deleteEmployee,但还是没有任何反应。
  • 你必须改变它的功能和调用它
  • 两者都改变​​了。还是没有运气。
  • 您检查错误了吗?我还添加了更简单的解决方案
  • 好的,谢谢,我的 javascript onclick 函数现在可以工作了,我收到了确认消息,但是在单击“是”后,我收到 404 Page Not Found 错误。
【解决方案3】:

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

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

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

在您的情况下,uri 段值是 3

【讨论】:

  • 再次感谢@hardik。但现在我收到 404 page not found 错误。
  • 你检查你的数据库行是否被删除??
  • 因为它看起来可能是重定向页面的问题。
  • 我签入了我的数据库。数据不会被删除。
  • 你在浏览器中检查了 url 吗??
【解决方案4】:

删除按钮也遇到了同样的问题,最后我弄清楚了这些代码,它完全按照我想要的方式工作。

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

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多