【问题标题】:Confirm box in CodeIgniter anchor link to delete recordCodeIgniter 锚链接中的确认框以删除记录
【发布时间】:2013-02-20 20:06:55
【问题描述】:

我正在使用以下代码从数据库中删除一条记录。但我面临一个问题:当我在确认框中单击“取消”时,它会删除记录。如果我点击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();"));
?>

【问题讨论】:

    标签: php javascript codeigniter


    【解决方案1】:

    一行一行

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

    【讨论】:

    • 这对我有用,但是添加一个图标而不是“删除”文本怎么样?
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • @jamshidHashimi 但是如何通过post方法通过表单提交删除记录?上述解决方案发出获取请求。
    【解决方案3】:

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

    【讨论】:

    • 你能得到这样一个简单的例子吗?呈现的 HTML 应如下所示:&lt;a href="user/deleteuser/123" onclick="return confirmDialog();"&gt;Delete&lt;/a&gt;
    【解决方案4】:

    在您的视图中:

    <?= 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回答Here

    【讨论】:

      【解决方案5】:

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

      <a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>
      

      【讨论】:

        【解决方案6】:

        试试这个

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

        【讨论】:

          【解决方案7】:
           <?= 
                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 是通过控制器中可用的方法传递的变量。

          【讨论】:

            【解决方案8】:

            最后,如果你想要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>    
                
            

            【讨论】:

              猜你喜欢
              • 2014-09-11
              • 1970-01-01
              • 1970-01-01
              • 2020-12-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-11-15
              • 1970-01-01
              相关资源
              最近更新 更多