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();"));
?>
4

8 回答 8

12

一切尽在一条线上

<?=anchor("user/deleteuser/".$row->id,"Delete",array('onclick' => "return confirm('Do you want delete this record')"))?>
于 2013-10-21T11:28:29.623 回答
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>
于 2013-03-06T07:06:16.513 回答
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>
于 2013-03-06T06:34:43.870 回答
1

在您的视图中:

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

或者

<?=
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 答案

于 2018-06-12T01:04:09.847 回答
0

我在我的代码中使用了这个

<a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>
于 2013-03-06T06:32:57.603 回答
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;
    }
于 2018-03-12T09:14:01.497 回答
0

最后,如果你想要javascript确认:所以当用户点击时,它会要求确认,然后如果用户点击ok就去删除。您在该行已被删除时要求确认。

在查看页面中:-

<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>    
    
于 2020-08-11T06:28:07.453 回答
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();
    ?>

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

于 2018-10-14T15:41:37.710 回答