0

first of all, i already trying maybe all of the possible answer in this site but nothing is working for me.

i want to show pop up windows confirmation before delete my data. i use this

 <a href="<?php echo site_url('admin/barang/delete/'.$val->idBarang);?>" onclick="return confirm('Are you sure ?')">Delete</a>

Maybe you need my controller

public function delete($id = 0) {
    $id OR redirect(site_url('admin/barang'));

    $this->barang_m->delete($id);

    redirect(site_url('admin/barang'));
}

My model barang_m

public function __construct(){
    parent::__construct();
    parent::set_table('barang','idBarang');
}
public function delete($id = 0) {
    if($data = parent::get($id)) {
        parent::delete($id);
        return true;
    }
    return false;
}

but when i click cancel it still delete the data even when i clicked 'x' to close the confirmation window. i'm so frustated please help me. Or maybe you can tell me why this problem occurs ?

**i alrady tried this possible answer
Codeigniter when i click delete i want pop up notification if if click yes or not
jQuery delete confirmation box
Confirm box in CodeIgniter anchor link to delete record and many more

Community
  • 1
  • 1
Noname
  • 33
  • 1
  • 7

4 Answers4

1

You need to stop page from opening link when you press cancel. But as seen in code its looks like you missed condition to check so. Try this may solved you problem.

<a href="<?php echo site_url('admin/barang/delete/'.$val->idBarang);?>" onclick="isconfirm();">Delete</a>

<script>
function isconfirm(){

if(!confirm('Are you sure ?')){
event.preventDefault();
return;
}
return true;
}
</script>
Prashant
  • 375
  • 2
  • 8
  • Thank you for your answer, i think yours should work but it doesnt work T_T when i clicked cancel it still deleted the data. i add my controller and model in my question, maybe there's a problem in it. – Noname Nov 20 '16 at 15:40
1

try

<a href="javascript:;" onclick="return isconfirm('<?php echo site_url("admin/barang/delete/".$val->idBarang); ?>');">Delete</a>

and your function like this

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

You could also try

<a href="<?php echo site_url("admin/barang/delete/".$val->idBarang); ?>" class="confirmClick">Delete</a>

And js would be

$('.confirmClick').click(()=> {
  var sure = confirm('Are you sure ?');
  if(sure){
    return true;
  }
  return false;
})
Deepak M
  • 849
  • 8
  • 17
  • why not onclick="return confirm('are you sure')" ? Dont need a seperate function for that ^^ – RFLdev Nov 23 '16 at 11:48
  • However `onclick="return confirm('are you sure..?')" ` working fine for me. In your case, I can't find what's the problem is..... :( – Deepak M Nov 23 '16 at 12:16
0

It's because you are running it out of the button itself and returning it. So you're saying, delete the button and return the confirmation.

Probably deleting the return will work, or make a function and make sure it doesn't return it but puts what you want in a variable or something.

Paul de Koning
  • 422
  • 1
  • 5
  • 16
0
<a href="javascript:;" onclick="return confirm_Action('<?php echo base_url(); ?>index.php/admin/delete_department/<?php echo $dept['Dept_id']; ?>');" ><button> DELETE</button></a>

Then, Your function confirm_Action could be like this:

//THis Allows Me To Control The Delete Actions
function confirm_Action(url){
// alert(url);
if(confirm('Are You Sure, This Action Can\'t Be Reversed?') == false)
{
    return false;
}
else
{
    location.href=url;
}

}

just like how @M. Deepak did it...

Thanks... Hope this helps

Max BigBudget
  • 71
  • 1
  • 8