Postingan lainnya
Hapus Data dengan ajax
Halo, Saya ingin membuat tombol hapus dengan ajax yang mana tombol hapus ini akan menghapus bagian html beserta data yang tersambung di database, nah saat saya membuat query nya,tombol hapus nya tidak berfungsi, saya coba untuk var dump namun tidak muncul error apapun.. Kira" solusinya bagaimana ya? terimakasih
Kode timeline.php
<?php
require_once("auth.php");
//include "koneksi.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="icon" type="image/png" href="Assets/icon.png">
<link rel="stylesheet" type="text/css" href="Assets/timeline.css">
<title>Timeline HeadBook</title>
<script type="text/javascript" src="Assets/jquery.js" ></script>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body class="bg-light">
<div class="container mt-5" style="height:100%;">
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-body text-center">
<img class="img img-responsive rounded-circle mb-3" width="160" src="Assets/<?php echo $_SESSION['user'][7] ?>" />
<h3><?php echo $_SESSION['user'][1]." ".$_SESSION['user'][2] ?></h3>
<p><?php echo $_SESSION['user'][3] ?></p>
<p><a href="logout.php">Logout</a></p>
</div>
</div>
</div>
<div class="col-md-8">
<form action="" method="post" >
<div class="form-group">
<textarea type="text" name="Nama" style="display:none;"><?php echo $_SESSION['user'][3] ?> </textarea>
<textarea class="form-control" placeholder="Apa yang kamu pikirkan?" name="status"></textarea>
<br>
<button type="submit" class="btn btn-danger" name="tombol">Bagikan</button>
</div>
<?php
require_once("config.php");
if(isset($_POST['tombol'])){
$email = $_REQUEST['Nama'];
$status = $_REQUEST['status'];
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ("Koneksi gagal");
$mysqli = "INSERT INTO timeline (Email, Status) VALUES ('$email', '$status')";
$result = mysqli_query($conn, $mysqli);
//$rows = $result->fetch(PDO::FETCH_NUM);
//$_SESSION['user1'] = $rows;
//echo "<script>history.back()</script>";
} ?>
<div>
<?php
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ("Koneksi gagal");
$select = mysqli_query($conn, "SELECT * FROM timeline ORDER BY id DESC");
while($stat = mysqli_fetch_array($select)){ ?>
<?php $str = $stat['id']; ?>
<div class='' id='komentar_<?=$str;?>' data-id='<?=$stat['id'];?>'>
<?php
echo "
<div class='card mb-3' id='komentar_<?=$str;?>' data-id='<?=$str;?>'>
<div class='card-body'>
<div class='row' style='background-color:#9F3'>
<div class='media'>
<img src='Assets/default.svg' class='media-object img-rounded' width='50' height='50' alt='Sample Image'>
<div class='media-body'>
<h4 class='media-heading'><small>".$stat['Email'].' | '.$stat['Waktu'].
"<i></i></small></h4>
<br><hr>
<p>".$stat['Status']."</p>
</div>
</div>
<div class='row'>
</div>
</div>
<input type='submit' value='Edit'>
<button type='button' class='hapus_komentar' data-id='<?=$str;?>'>Hapus</button>
</div>
</div>
</div>
<br>
"
;}
?>
</div>
<?php mysqli_close($conn);
?>
<script type="text/javascript">
$(document).on('click','.hapus_komentar',function(){
var id = $(this).attr('data-id');
$.ajax({
method: "POST",
url: "Hapus.php",
data: { id_komen:id , type:"delete"},
});
});
</script>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Kode Hapus.php
<?php
require_once("config.php");
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name) or die ("Koneksi gagal");
if($_POST['type'] == 'delete'){
$query = "DELETE FROM timeline WHERE id=".$_POST['id_komen'];
die(var_dump($_POST['id_komen']));
}
?>
1 Jawaban:
Jawaban Terpilih
coba tangkap errornya di ajax, contoh <pre> $(document).on('click','.hapus_komentar',function(){ var id = $(this).attr('data-id'); $.ajax({ method: "POST", url: "Hapus.php", data: { id_komen:id , type:"delete"}, success: function(data){ console.log(data); } }); }); </pre> trus di phpnya biar simple, dibuat variable, data yg dari ajax <pre> $id = $_POST['komen_id']; if($_POST['type'] == 'delete'){ $query = "DELETE FROM timeline WHERE id= $id"; $result = mysqli_query($conn, $query); if($result){ echo "horee berhasil hapus data"; }else{ echo "yah gagal hapus data"; } </pre> semoga membantu ^^
Tanggapan
Udah coba console.log tapi masih tidak terjadi apa", itu gimana ya??
dibagian phpnya ubah
if($_POST['type'] == 'delete'){ $id = $_POST['komen_id'];
Saat saya coba hapus datanya, muncul else echo "yah gagal hapus data"; berarti salahnya dimana ya bang?
Coba diajaxnya di cek dulu id nya sesuai gk sm yg di database. di console.log(id)
Akhirnya berhasil, Terimakasih bang sudah membantu saya untuk menyelesaikan project kampus