[英]How to delete record from MySQL database using PHP and AngularJS?
[英]How to delete a record from MySQL database using AJAX and PHP in Codeigniter
我想使用AJAX從MySQL數據庫表中刪除記錄。 我用PHP完成它並且工作正常。 但我無法通過AJAX實現這一目標。
AJAX
$(document).ready(function() {
$(".confirm").click(function() {
var bid = $(this).closest("div.box2").find('input[name="dbid"]').val();
var dataString = 'id=' + bid;
$.ajax({
type: "POST",
url: "<?php echo site_url('user/delete_article')?>",
data: dataString,
cache: false,
success: function() {
$.alert('Confirmed!');
}
});
});
});
PHP
public function delete_article($id){
$data['success']='';
$data['error']='';
include_once ('query/user_query.php');
$this->db->where('bid', $id);
$data['countEarticle'] = $this->db->count_all_results('blog');
if($data['countEarticle'] >= 1){
$this->db->where('bid',$id);
$this->db->delete('blog');
}
if($data['countEarticle'] <= 0){
}
}
HTML
<div class="box-footer box-comments box2" style="display: block;">
<input type="hidden" name="dbid" value="<?php echo $draftfull['bid']?>">
<p>
<btn class="btn btn-azure btn-sm confirm"><i class="fa fa-trash-o"></i>Delete Article</btn>
</p>
</div>
我需要你的幫助。 我究竟做錯了什么?
試試這段代碼: -
public function delete_article(){
$id=$this->input->post('bid');
$data['success']='';
$data['error']='';
include_once ('query/user_query.php');
$this->db->where('bid', $id);
$data['countEarticle'] = $this->db->count_all_results('blog');
if($data['countEarticle'] >= 1){
$this->db->where('bid',$id);
$this->db->delete('blog');
}
if($data['countEarticle'] <= 0){
}
}
嘗試這個:
<a href="javascript:;" class="btn btn-danger btn-xs mt-sweetalert swtalert" onclick="delete('<?php echo $draftfull["bid"] ?> ')" title="Delete"><span class="fa fa-ban"></span></a>
// AJAX
function delete(id) {
swal({
title: "Are you sure to delete?",
text: "Deleting will remove row from listing!",
type: "error",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes!",
cancelButtonText: "No",
closeOnConfirm: true,
closeOnCancel: true
}, function (isConfirm) {
if (isConfirm) {
$.post(
base_url + "user/delete_article",
{bid: id},
function (data) {
if (data === "1") {
location.reload();
} else if (data === "0") {
swal("", "Error to deleting data.", "warning");
} else {
swal("", data[0], "error");
}
});
}
});
}
//控制器
function delete_article($id){
if ( $this->model_name->deleteDataById($this->input->post('bid') ) {
die('1');
}
die('0');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.