jQuery Ajax 함수를 사용하여 MySQL 데이터베이스의 행을 삭제하려고합니다. 게시물 "게시물 삭제"링크를 클릭하면 행 삭제 기능을 실행하는 onClick
이벤트가 트리거됩니다.Ajax 삭제 기능이 작동하지 않습니다.
이 지금까지 내 코드입니다 :
는 첫째로 각 하나를 삭제하는 링크와 함께 게시물을 표시합니다. 이어서
foreach($postarray AS $value){
echo '<div class="item">'.$value['post_title'].' , <a href="#" class="delete_link" value="'. $value['post_id'] .'">Delete Post</a></div>';
}
JQuery와 :
$(document).ready(function(){
$(".delete_post").click(function(){
var id = $(this).attr("value");
function deletePost(id){
$.ajax({
type: "POST",
url: "delete_post.php",
data: {id: id},
success: function(data){
alert("Post deleted");
}
})
}
});
});
및 delete.php
번호 :
//Start the session
session_start();
require_once('func/auth.class.php');
require_once('func/functions.php');
require_once('func/db.class.php');
// Check if user is logged in
if(!$_SESSION['is_admin'] || empty($_POST['id'])){
header("Location: index.php");
}
$post_id = $_POST['id'];
delete_post($post_id);
및 delete_post()
기능 :
function delete_post($post_id){
global $dbh;
$stmt = $dbh->prepare("DELETE FROM mjbox_posts WHERE post_id = ?");
$stmt->bindValue(1, $post_id, PDO::PARAM_INT);
$stmt->execute();
if($stmt->rowCount() != 0){
return TRUE;
}else{
return FALSE;
}
}
현재이 방법 delet되지 데이터베이스에서 게시물을 보내고 나는 왜 작동하지 않을 수 있습니다.
내가 나중에, 난 단지 "delete_link"클래스 참조 jcho360 @ 클래스' "delete_post"' – jcho360
이 표시되지 않는 –
내 나쁜 변경하는 것을 잊었다 당신이 볼 수있는 아약스 응답이 무엇 스택 – crm