[英]Delete Entry From MySQL Table Using AJAX
As I said in another question in here, I'm creating a login class, but I'm making some additional options for it.正如我在这里的另一个问题中所说,我正在创建一个登录类,但我正在为它做一些额外的选择。
One of which is deleting users.其中之一是删除用户。 I'll show you the code I'm using to do this.
我将向您展示我用来执行此操作的代码。
First I list all the users using this function :首先我列出所有使用这个函数的用户:
public function listUsers() {
$sql = "SELECT username FROM user";
if($stmt = $this->conn->prepare($sql)) {
$stmt->bind_result($usrn);
$stmt->execute();
while ($row = $stmt->fetch()) {
$stmt->bind_result($usrn);
echo '<form name="delete-user" id="'.$usrn.'" class="delete-user" method="post" action="#">'.
'<fieldset class="user-wrapper">'.
'<label for="user" class="user-label">User</label>'.
'<input type="text" name="user" class="user" value="'.$usrn.'" autocomplete="off" readonly="readonly" />'.
'</fieldset>'.
'<fieldset class="delete-wrapper">'.
'<button type="submit" name="delete" class="delete">Delete</button>'.
'</fieldset>'.
'</form>';
}
$stmt->close();
echo '<div class="message-handling"></div>';
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "The Users Could Not Be Listed";
return json_encode($message);
}
}
As you can see the result of this function in the HTML will be like this:如您所见,此函数在 HTML 中的结果将如下所示:
<form name="delete-user" id="altceva" class="delete-user" method="post" action="#">
<fieldset class="user-wrapper">
<label for="user" class="user-label">User</label>
<input type="text" name="user" class="user" value="altceva" autocomplete="off" readonly="readonly" />
</fieldset>
<fieldset class="delete-wrapper">
<button type="submit" name="delete" class="delete">Delete</button>
</fieldset>
</form>
The id of the form is actually the name of each user (for each user a form is created with the user's id).表单的 id 实际上是每个用户的名称(为每个用户创建一个带有用户 id 的表单)。 Also the value of the input has the user name too, because I need it to pass it to another function so I can delete that user.
输入的值也有用户名,因为我需要它将它传递给另一个函数,以便我可以删除该用户。
Next I'm trying to use AJAX to delete the user I want to delete.接下来我尝试使用 AJAX 删除我要删除的用户。 I have this function so far:
到目前为止我有这个功能:
var ids = $('.delete-user-content form[id]').map(function(){
return this.id;
}).get();
jQuery.each(ids, function() {
console.log("#" + this + ' .delete');
$("#" + this + ' .delete').click(function() {
$('.delete-user-content .message-handling').fadeOut();
$.ajax({
type: 'POST',
url: 'assets/class/login/process.php',
dataType: 'json',
data: {
delusername: $('.delete-user input.user').val()
},
success:function(data) {
if(data.error === true) {
$('.delete-user-content .message-handling').text(data.message).fadeIn();
console.log(data.message);
setTimeout(function () {
$('.delete-user-content .message-handling').fadeOut();
}, 15000);
}
else {
$('.delete-user-content .message-handling').text(data.message).fadeIn();
console.log(data.message);
$('#'+id).fadeOut();
array.splice(index, 1);
setTimeout(function () {
$('.delete-user-content .message-handling').fadeOut();
}, 15000);
}
},
error:function(XMLHttpRequest,textStatus,errorThrown) {
$('.delete-user-content .message-handling').text('Error | Check PHP log for information').fadeIn();
console.log('Error | Check PHP log for information');
}
});
return false;
});
});
And the PHP function to process the action is this one:处理动作的 PHP 函数是这样的:
public function deleteUser($user) {
$sql = "DELETE FROM user WHERE username = '$user'";
if($stmt = $this->conn->prepare($sql)) {
$stmt->execute();
$stmt->close();
$error = false;
$message['error'] = false;
$message['message'] = "The User ".$user." Has Been Deleted";
return json_encode($message);
}
else {
$error = true;
$message['error'] = true;
$message['message'] = "The User Could Not Be Deleted";
return json_encode($message);
}
}
But the thing is that this doesn't work properly.但问题是这不能正常工作。 One problem would be if I delete the last listed user it will actually delete the first user.
一个问题是,如果我删除最后一个列出的用户,它实际上会删除第一个用户。 Then after I make one delete, if I click on another delete nothing else will be deleted, it will just tell me that the same user as the first time was deleted.
然后在我删除一次之后,如果我点击另一个删除,其他任何东西都不会被删除,它只会告诉我与第一次删除的是同一个用户。
Now, I know this is a lot of code and it's hard to understand it, but could someone help me out with it?现在,我知道这是很多代码并且很难理解,但是有人可以帮我解决吗?
It seems you are using a very complicated way to determine your user ID.您似乎正在使用一种非常复杂的方法来确定您的用户 ID。 Why don't you just add a hidden field to the form and attach the event handler to the form?
为什么不直接在表单中添加一个隐藏字段并将事件处理程序附加到表单中呢? That would eliminate the need for the
map
and each
, speeding everything up in the process.这将消除对
map
和each
的需要,加快过程中的一切。
So you form would be something like:所以你的表格会是这样的:
<form name="delete-user" class="delete-user" method="post" action="#">
<input type="hidden" name="user_id" value="PHP_ECHO_USER_ID" />
<fieldset class="user-wrapper">
...
And your jquery would be simplified to:您的 jquery 将简化为:
$("form.delete_user").submit(function() {
var user_id = $(this).find('[name="user_id"]').val(); // on the top of my head, untested...
console.log(user_id); // make sure you get the right user ID before doing anything else
...
return false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.