我使用以下代码从数据库中删除记录。但是我遇到了一个问题:当我点击确认框中的“取消”时,它会删除记录。如果我点击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();"));
?>
答案 0 :(得分:12)
一行中的所有内容
<?=anchor("user/deleteuser/".$row->id,"Delete",array('onclick' => "return confirm('Do you want delete this record')"))?>
答案 1 :(得分:7)
试试这个:
<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>
答案 2 :(得分:4)
您是否在控制台中收到任何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>
答案 3 :(得分:0)
我在我的代码中使用它
<a href='<?php site_url('controler/function/$id');?>' onClick='javascript:return confirm(\"Are you sure to Delete?\")'>Delete</a>
答案 4 :(得分:0)
试试这个
<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;
}
答案 5 :(得分:0)
在查看:
中<?= 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
答案 6 :(得分: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();
?>
这是删除代码。首先,我们打开表单admin是我们的控制器名称,delete_noti是admin控制器内部的函数。我们以隐藏形式发送ID。 $ notification是通过控制器中可用的方法传递的变量。
答案 7 :(得分:0)
最后,如果您要JavaScript确认:因此,当用户单击时,它会要求确认,然后如果用户单击“确定”,则删除。您在询问何时删除该行。
在查看页面中:-
<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>