Noname
Noname

Reputation: 33

Javascript delete confirmation box in codeigniter

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

Upvotes: 0

Views: 3863

Answers (4)

Deepak M
Deepak M

Reputation: 849

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

Upvotes: 1

Max BigBudget
Max BigBudget

Reputation: 71

<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

Upvotes: 0

Prashant
Prashant

Reputation: 385

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>

Upvotes: 1

Paul de Koning
Paul de Koning

Reputation: 532

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.

Upvotes: 0

Related Questions