7

I am using the following code to delete a record from a database. But I am facing a problem: when I click on "cancel" in the confirm box then it deletes the record. If I click cancel it returns false but how does it delete the record?

What am doing wrong?

Javascript code:

function ConfirmDialog() {
  var x=confirm("Are you sure to delete record?")
  if (x) {
    return true;
  } else {
    return false;
  }
}

PHP code in view:

<?php
  echo anchor('user/deleteuser/'.$row->id, 'Delete', array('class'=>'delete', 'onclick'=>"return ConfirmDialog();"));
?>
dda
  • 6,030
  • 2
  • 25
  • 34
Kango
  • 809
  • 11
  • 27
  • 48

8 Answers8

12

Everything in one line

<?=anchor("user/deleteuser/".$row->id,"Delete",array('onclick' => "return confirm('Do you want delete this record')"))?>
Jamshid Hashimi
  • 7,639
  • 2
  • 28
  • 27
8

Try This:

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

and use this in your script:

<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>
Rahul Chipad
  • 2,373
  • 17
  • 20
5

Are you getting any Javascript errors in the console? I suspect that some other Javascript may be interfering. This simple test page works fine:

<?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>
Matt Browne
  • 12,169
  • 4
  • 59
  • 75
1

In your View:

<?= 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 in your View:

<script>
function confirm(){
  job=confirm("Are you sure to delete permanently?");
if(job!=true){
  return false;
  }
}
</script>

See webGautam answer Here

fWd82
  • 840
  • 1
  • 13
  • 31
0

I use This in my code

<a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>
vijaykumar
  • 4,658
  • 6
  • 37
  • 54
0

Try this

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

Then your function will be like:

function isconfirm(url_val){
    alert(url_val);
    if(confirm('Are you sure you wanna delete this ?') == false)
    {
        return false;
    }
    else
    {
        location.href=url_val;
    }
Max BigBudget
  • 71
  • 1
  • 8
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();
    ?>

This is the deletion code. first of all we open form admin is our controller name and delete_noti is the function inside admin controller. We are sending id as hidden form. Here $notification is the variable which is passed through method which is available in controller.

Sumit Kumar Gupta
  • 2,132
  • 1
  • 22
  • 21
0

Finally, if you want javascript confirm: so when the user clicks, it asks to confirm and then goes to delete if the user clicks okay. You're asking confirmation when the row has already been deleted.

In View Page :-

<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>

Controller Code:-

<?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');
    }
}
?>  

Then Show Success Message on View:-

<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>    
    
KUMAR
  • 1,993
  • 2
  • 9
  • 26