Case Study : Create function to delete multiple data on the database with checkbox component
Requirements : Webserver Packages, already installed.

Multi-delete Checkbox like in Gmail, we can choose to delete a message in our inbox smply by clicking the checkbox on the data that we want to remove. This tutorial will create a sample application to remove the data – specific data which is marked by checkbox (like in Gmail).

Ok, let’s get into the tutorial.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Prepare a table named tb_kota, with structure like picture below,
  3. Insert some sample data like below,
  4. Done!

Step 2 : Prepare the work directory

  1. Create folder named tutorphp in your document root
  2. Save all file in this tutorial in this folder.

Step 3 : Create a connection to DB script

  1. Type the following script,
    <?php
    $host = "localhost";
    $user = "root";//adjust according to your mysql setting
    $pass = ""; //adjust according to your mysql setting, i use no password here
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed !! : ".mysql_error());
    ?>
    
  2. Save with the name connect.php

Step 4 : Script to Show the Data

  1. Type the following script,
    <html><head>
    <title>Itx.Web.Id</title>
    <script type="text/javascript" src="pilihan.js"></script>
    <script type="text/javascript" src="warning.js"></script>
    </head>
    <body>
    <h2> The Data <h2>
    <?php
    include 'connect.php';
    $query = "SELECT * FROM tb_book"; //the query for get all data in tb_student
    $result = mysql_query($query);
    ?>
    
    <form name="myform" method="post" action="prosesdeletemulti.php">
    			
    <?php
    echo "<table border='0' cellpadding='2' cellspacing='2'>";
    echo "<tr bgcolor='orange'>
            <td> <b> Title </b> </td>
            <td> <b> Author </b> </td>
            
         </tr>";
    	 
    $i = 0;
    while ($data = mysql_fetch_array($result)) { //mysql_fetch_array = get the query data into array
      echo "<tr>
                <td>".$data['title']."</td>
                <td>".$data['author']."</td> "; ?>
    <td><input type="checkbox" name="id[<?php echo $i;?>]" value="<?php echo $data['id'];?>" /></td>
    <?php
    $i++;
    }
    echo "</table>";
    ?>
    <td> <input type="submit" value="Delete" onClick="return warning();" name="submit">
    <input type="reset" value="Cancel" name="reset"> </td>
    </form>
    </body>
    
    </html> 
  2. save with the name student_data1.php

Step 5 : Create javascript to show warning

  1. Type the following script,
    function warning() {
    	return confirm('Are You Sure to Delete This Data?');
    }
    
  2. save with the name warning.js

Step 6 : Javascript to get the checkbox ed component

  1. Type the following script,
    function pilihan()
      {
         // read the component from 'myform'
         var jumKomponen = document.myform.length;
         // no check all
         if (document.myform[0].checked == false)
            {
                for (i=1; i<=jumKomponen; i++)
                {
                   if (document.myform[i].type == "checkbox") document.myform[i].checked = false;
                } 
            }
      }
    
  2. save with the name pilihan.js

Step 7 : Script to process delete multiple data

  1. Type the following script,
    <?php
    include 'connect.php';
    $data = $_POST['id']; //get all the id book that will be deleted
    
    foreach($data as $data1) { //looping according to the total data that checked
    	//echo $data1;
    	$query = "DELETE FROM tb_book where id = $data1"; //the query to delete data according to id
    	$result = mysql_query($query);
    }
    if ($result) {
    	include "student_data1.php";
    	echo "Delete sucess";
    }
    ?>
    
  2. save with the name prosesdeletemulti.php

Step 8 : Testing Code

  1. Go to http://localhost/tutorphp/student_data1.php. Check the checkbox on the data where you will delete the data.
  2. Warning appear. Choose Ok.
  3. Success confirmation appear, and the data really has been deleted. Check your database also :D

For the script explanation.. see script comment. and feel free to comment :D

OK. Haaaaaaaapppy Coding! :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

1 Comment

Tinggalkan Balasan

Alamat surel Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: