Membuat Script Multiple Delete dengan Checkbox dan Konfirmasi Delete dengan Javascript dan PHP

Pada contoh berikut kita akan membuat script multiple delete data dari database, di mana kita menggunakan checbox untuk memilih data-data yang akan didelete. Kita juga akan membuat feature untuk check uncheck semua checkbox yang ada. Selain itu kita juga akan menampilkan window konfirmasi apakah data akan didelete atau tidak.

langsung aja kita lihat kodenya

index.php

<?php
include "koneksi.php";
?>
<html>
<head><title>Delete</title>
<script>
var jumlahnya;
function ceksemua(){
    jumlahnya = document.getElementById("jumlahcek").value;
    if(document.getElementById("cekbox").checked==true){
        for(i=0;i<jumlahnya;i++){
            idcek = "bukuid"+i;
            idtr = "tr"+i;
            document.getElementById(idtr).style.backgroundColor = "#efefef";
            document.getElementById(idcek).checked = true;
        }
    }else{
        for(i=0;i<jumlahnya;i++){
            idcek = "bukuid"+i;
            idtr = "tr"+i;
            document.getElementById(idtr).style.backgroundColor = "#FFFF99";
            document.getElementById(idcek).checked = false;
        }
    }
}

function konfirmasicek(indeks){
    idcek = "bukuid"+indeks;
    bukuidnya = document.getElementById(idcek).value;
    tanya = confirm("Delete Buku dengan ID "+bukuidnya+"?");
    if(tanya == 1){
        window.location.href="delete.php?op=delsatu&id="+bukuidnya;
    }
}

function konfirmasicek2(){
    ada = 0;            //untuk mengecek apakah ada checkbox yang dicek
    semuanyakah = 1;    //untuk mengecek apakah semua checkbox tercek

    //untuk mengambil jumlah total checkbox yang ada
    jumlahnya = document.getElementById("jumlahcek").value;

    jumlahx = 0         //untuk mengetahui jumlah yang dicek
    for(i=0;i<jumlahnya;i++){
        idcek = "bukuid"+i;
        if(document.getElementById(idcek).checked == true){
            jumlahx++;
            ada = 1;
        }else{
            semuanyakah = 0;
        }
    }
    if(ada==1){
        if(semuanyakah == 1){
            tanya = confirm("Mau delete semuanyakah?");
            if(tanya == 1){
                document.getElementById("formulirku").submit();
            }
        }else{
            tanya = confirm("Mau delete data "+jumlahx+" item ?");
            if(tanya == 1){
                document.getElementById("formulirku").submit();
            }
        }
    }
}

function setwarna(indeks){
    idcek = "bukuid"+indeks;
    idtr = "tr"+indeks;
    if(document.getElementById(idcek).checked == true){
        document.getElementById(idtr).style.backgroundColor = "#efefef";
    }else{
        document.getElementById(idtr).style.backgroundColor = "#FFFF99";
    }
}
</script>
</head>
<body bgcolor="#FFFF99">
<?php
if($_GET['op']=="berhasildelete"){
    echo "<b><font color=red>Data berhasil didelete</font></b><br>";
}
?>
<form action=delete.php method=post id=formulirku>
<table border="1" cellpadding="3" cellspacing="0" bgcolor="#FFFF99"
style="border-collapse: collapse" bordercolor="#FFCC00">
  <tr>
    <td bgcolor="#FF9900"><input type="checkbox" onclick="ceksemua()" id="cekbox"></td>
    <td bgcolor="#FF9900"><b>ID Buku</b></td>
    <td bgcolor="#FF9900"><b>Judul Buku</b></td>
  </tr>
<?php
$databuku = mysql_query("SELECT * FROM buku");
$indexcek = 0;
while($d = mysql_fetch_array($databuku)){
    echo "<tr id='tr$indexcek'><td><input type='checkbox' name='bukuid[]'
    value='".$d['bukuid']."' id='bukuid$indexcek' onclick='setwarna($indexcek)'>
    <img src='delete.png' onclick=\"konfirmasicek('$indexcek')\"
    style='cursor:pointer'>\n";
    echo "<td>".$d['bukuid']."</td><td>".$d['judul']."</td></tr>\n";
    $indexcek++;
}
echo "<input type=hidden id='jumlahcek' value='$indexcek' name='jumlahcek'>";
?>
</table>
<input type="button" value="delete" onclick="konfirmasicek2()">
</form>
</body>
</html>

delete.php

<?php
include "koneksi.php";

$op = $_GET['op'];

if($op){
    $id = $_GET['id'];
    $del = mysql_query("DELETE FROM buku WHERE bukuid='$id'");
}else{
    foreach($_POST['bukuid'] as $value){
        $del = mysql_query("DELETE FROM buku WHERE bukuid='$value'");
    }
}

if($del){
    header("location:index.php?op=berhasildelete");
}else{
    echo "error";
}
?>

download source code

dikutip dari

http://blog.codingwear.com/read34-Membuat-Script-Multiple-Delete-dengan-Checkbox-dan-Konfirmasi-Delete-dengan-Javascript-dan-PHP.drz

Advertisement
    • Ariyanto
    • February 2nd, 2011

    wah yang ini berat nih .. aku nda ngarti .. hahaha ..

      • choirulVandross
      • February 2nd, 2011

      beratan mana sm mas?? hehehehe ..

    • Sarah
    • February 2nd, 2011

    ampuuunn ay .. hahhaha .. :P

    • choirulVandross
    • February 4th, 2011

    silahkan sedoot maas

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 136 other followers