我试图通过单击图标从我的数据库中删除一行。简而言之,它不起作用,我无法弄清楚原因。阅读这里的许多相关问题以及网络上的一些教程。仍然是php和ajax的新手,所以请原谅noob错误,如果有的话。谢谢你们的干杯。
$('.delete').click(function(){
// get resource id to use for deleting
var del_id = $(this).attr('data-resource-id');
// get parent
var parent = $(this).parent().parent();
var transitionEnd = 'transitionend webkitTransitionEnd oTransitionEnd otransitionend';
$.post('includes/delete-resource.php', {id:del_id},function(data){
// animation out
parent.addClass('remove-resource').one(transitionEnd, function(){
parent.remove();
}); // end addClass
}); // end post
}); // end function
require_once('config.php');
//connect to $db
require_once('database.php');
// Fetch posted id sent from click
$resource_id = $_POST['id'];
$safe_id = filter_var($resource_id,FILTER_SANITIZE_INT);
try {
// prepare Statement
$remove = $db->prepare(
"DELETE FROM resources
WHERE resource_id = :safe_id");
// Bind
$remove->bindParam(':safe_id', $safe_id);
// run query
$remove->execute();
} catch (Exception $e) {
echo "Data could not be removed";
exit;
}
if($remove -> rowCount()){
echo 'success';
} else {
echo 'fail';
}
答案 0 :(得分:0)
如果.delete
元素是超链接,您可能希望在event.preventDefault()
的帮助下停止它的默认行为
$('.delete').click(function(e){
e.preventDefault();
// get resource id to use for deleting
// ...