在CodeIgniter中,确认框用于删除记录的锚链接

7
我正在使用以下代码从数据库中删除记录。但是我遇到了一个问题:当我在确认框中单击“取消”时,它会删除记录。如果我单击cancel,它会返回false,但是它如何删除记录?
我做错了什么?
JavaScript代码:
function ConfirmDialog() {
  var x=confirm("Are you sure to delete record?")
  if (x) {
    return true;
  } else {
    return false;
  }
}

视图中的PHP代码:

<?php
  echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return ConfirmDialog();"));
?>
8个回答

12

所有内容在一行中

<?=anchor("user/deleteuser/".$row->id,"Delete",array('onclick' => "return confirm('Do you want delete this record')"))?>

这对我有用,但是换成图标而不是“删除”文本怎么样? - claudios

8

试试这个:

<a href="javascript:void(0);" onclick="delete(<?php echo $row->id;?>);">Delete</a>

并在你的脚本中使用:

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

@jamshidHashimi 但是如何通过表单提交的post方法删除记录?上述解决方案使用了get请求。 - RN Kushwaha

5

你的控制台里有没有出现任何JavaScript错误?我怀疑其他一些JavaScript可能会干扰。这个简单的测试页面可以正常工作:

<?php echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return confirmDialog();")); ?>

<script>
function confirmDialog() {
    return confirm("Are you sure you want to delete this record?")
}
</script>

你能够让这个简单的例子正常工作吗?渲染后的HTML应该是这样的:<a href="user/deleteuser/123" onclick="return confirmDialog();">删除</a> - Matt Browne

1
在你的视图中:
<?= anchor("admin/delete_article/{$article->id}", 'Test', ['name'=>'submit', 'value'=>'Delete', 'class'=>'btn btn-danger', 'onclick'=>'return confirm()']);
?>

OR

<?=
form_open('admin/delete_article'),
form_hidden('article_id', $article->id),
form_submit(['name'=>'submit', 'value'=>'Delete', 'class'=>'btn btn-danger', 'onclick'=>'return confirm()']),
form_close();
?>

在你的视图中的 JavaScript:
<script>
function confirm(){
  job=confirm("Are you sure to delete permanently?");
if(job!=true){
  return false;
  }
}
</script>

请看 webGautam 的答案这里

0
 <?= 
      form_open('admin/delete_noti'),
      form_hidden('noti_id',$notification->id),
     form_submit('submit','Delete',array('onclick' => "return confirm('Do you want delete this record')",'class'=>'btn btn-danger float-center')),
      form_close();
    ?>

这是删除代码。首先,我们打开管理员表单,它是我们的控制器名称,delete_noti 是 admin 控制器内的函数。我们将 id 作为隐藏表单发送。在这里,$notification 是通过可用于控制器的方法传递的变量。


0

我在我的代码中使用这个

<a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>

0

试试这个

<a href="javascript:;" onclick="return isconfirm('<?php echo site_url('user/deleteuser/'.$row->id); ?>');">Delete</a>

那么你的函数将会是这样的:

function isconfirm(url_val){
    alert(url_val);
    if(confirm('Are you sure you wanna delete this ?') == false)
    {
        return false;
    }
    else
    {
        location.href=url_val;
    }

0

最后,如果你想要 JavaScript 确认框:当用户点击时,它会询问确认,然后在用户点击确定后进行删除。你正在在行已经被删除时请求确认。

在视图页面中:

<td><a href="<?php echo base_url().'user/deleteuser/'.$user['user_id'];?>" class="btn btn-danger" onclick="return confirm('Are you sure want to Delete this User?')">DELETE</a></td>

控制器代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class user extends CI_Controller {

public function deleteuser($userid)
    {
      $this->db->delete('user', array('user_id'=>$userid)); 
      $this->session->set_flashdata('success','User deleted Successfully!!!');
            redirect(base_url().'admin/user');
    }
}
?>  

然后在视图上显示成功消息:

<div class="row col-md-12">
<?php 
   if($this->session->flashdata('success')){
 ?>
   <div class="alert alert-success "  style="display:none;"> 
     <?php  echo $this->session->flashdata('success'); ?>
<?php    
} else if($this->session->flashdata('error')){
?>
 <div class = "alert alert-danger"  style="display:none;">
   <?php echo $this->session->flashdata('error'); ?>
 </div>
<?php } ?>

</div></div>    
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接