php CodeIgniter 锚链接中的确认框以删除记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15240472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:52:28  来源:igfitidea点击:

Confirm box in CodeIgniter anchor link to delete record

phpjavascriptcodeigniter

提问by Kango

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 cancelit returns false but how does it delete the record?

我正在使用以下代码从数据库中删除记录。但我面临一个问题:当我在确认框中单击“取消”时,它会删除记录。如果我点击cancel它返回 false 但它如何删除记录?

What am doing wrong?

做错了什么?

Javascript code:

Javascript代码:

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

PHP code in view:

PHP代码查看:

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

回答by Jamshid Hashimi

Everything in one line

一切都在一行中

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

回答by Rahul Chipad

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>

回答by Matt Browne

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

您是否在控制台中收到任何 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>

回答by fWd82

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:

视图中的 JavaScript:

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

See webGautam answer Here

在此处查看 webGautam 答案

回答by Sumit Kumar Gupta

 <?= 
      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.

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

回答by Max Phoenix Newbie

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

回答by vijaykumar

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>