04 April, 2011

multiple delete record menggunakan checkbox

sesuai dengan judulnya multiple delete record yaitu menghapus beberapa record. pada artikel ini saya akan coba posting bagaimana cara menghapus beberapa atau semua record dengan menggunakan checkbox. cara kerjanya kurang lebih seperti pesan inbox atau outbox pada email, dapat menghapus satu pesan, beberapa, atau semua pesan. disini saya membuat contoh dengan data mahasiswa yang memiliki npm dan nama.


jujur saya bukan orang yang pandai berbicara dan juga tidak pandai berbuat dosa .hhihi
jadi kita langsung saja coba praktekan, come on lae ..
index.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 = "mhsid"+i;
      idtr = "tr"+i;
      document.getElementById(idtr).style.backgroundColor = "#F9F0DD";
      document.getElementById(idcek).checked = true;
    }
  }else{
    for(i=0;i<jumlahnya;i++){
      idcek = "mhsid"+i;
      idtr = "tr"+i;
      document.getElementById(idtr).style.backgroundColor = "#FFFFFF";
      document.getElementById(idcek).checked = false;
    }
  }
}

function konfirmasicek(indeks){

  idcek = "mhsid"+indeks;
  bukuidnya = document.getElementById(idcek).value;
  tanya = confirm("Hapus mahasiswa dengan npm "+bukuidnya+" ?");
  if(tanya == 1){
    window.location.href="delete.php?op=delsatu&id="+bukuidnya;
  }
}

function konfirmasicek2(){
  //mengecek apakah ada checkbox yang dicek
  ada = 0;

  //mengecek apakah semua checkbox tercek 
  semuanyakah = 1;   

  //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 = "mhsid"+i;
    if(document.getElementById(idcek).checked == true){
      jumlahx++;
      ada = 1;
    }else{
      semuanyakah = 0;
    }
  }

  if(ada==1){
    if(semuanyakah == 1){
      tanya = confirm("Anda yakin akan menghapus semua ?");
      if(tanya == 1){
        document.getElementById("formulirku").submit();
      }
    }else{
      tanya = confirm(jumlahx+" item akan di hapus, yakin ?");
      if(tanya == 1){
        document.getElementById("formulirku").submit();
      }
    }
  }
}

function setwarna(indeks){
  idcek = "mhsid"+indeks;
  idtr = "tr"+indeks;
  if(document.getElementById(idcek).checked == true){
    document.getElementById(idtr).style.backgroundColor = "#F9F0DD";
  }else{
    document.getElementById(idtr).style.backgroundColor = "#FFFFFF";
  }
}
</script>
</head>
<body>

<form action=delete.php method=post id=formulirku>
  <table border="1" cellpadding="3" cellspacing="0" bgcolor="#fff" style="border-collapse: collapse" bordercolor="#000000">
  <tr>
  <td bgcolor="#D9E2FD"><input type="checkbox" onClick="ceksemua()" id="cekbox"></td>
  <td bgcolor="#D9E2FD"><b>NPM</b></td>
  <td bgcolor="#D9E2FD"><b>Nama</b></td>
  <td bgcolor="#D9E2FD">&nbsp;</td>
  </tr>

<?
  $datamhs = mysql_query("SELECT * FROM mhs");
  $indexcek = 0;
  while($d = mysql_fetch_array($datamhs)){
    echo "<tr id='tr$indexcek'><td><input type='checkbox' name='mhsid[]' value='".$d['npm']."' id='mhsid$indexcek' onclick='setwarna($indexcek)'> ";
    echo "<td>".$d['npm']."</td><td>".$d['nama']."</td>\n";
    echo "<td><img src='delete.png' onclick=\"konfirmasicek('$indexcek')\" style='cursor:pointer'>\n</tr></td>";
    $indexcek++;
  }
  echo "<input type=hidden id='jumlahcek' value='$indexcek' name='jumlahcek'>";
?>
</table>
  <input type="button" value="delete" onClick="konfirmasicek2()">
</form>

<?
  if($_GET['op']=="berhasildelete"){
    echo "<div style='background-color:#fff; padding:5px;'><font color=#123456>Data berhasil dihapus</font></b></div>";
  }
?>
</body>
</html>

koneksi.php
<?php
  mysql_connect("localhost","root","1234567");
  mysql_select_db("multi_delete");
?>

delete.php
<?php
  include "koneksi.php";
  $op = $_GET['op'];

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

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

silahkan download scriptnya disini.
Load disqus comments

0 komentar