Tag input checkbox merupakan masukan yang memiliki opsi atau pilihan jika kita check (centang maka akan terdapat pilihan jika tidak maka akan di biarkan saja. Untuk memilih hanya satu pilihan maka di gunakan input radio namun jika pilihan lebih dari satu menggunakan checkbox. Nah kali ini admin akan menjelaskan bagaimana cara membuat CRUD checkbox dengan PHP. langsung saja kita praktekan teman teman.
Langkah-langkahnya
1. Pertama kalian buat database dan tabelnya terlebih dahulu di PHP myadmin nya seperti gambar di bawah ini
Note : Terlebih dahulu buat nama folder di dalam folder xampp/htdocs
2. Lalu buat koneksinya dan beri nama dengan nama config.php
<?php
$server = "localhost";
$user = "duniacermin21";
$pass = "duniacermin21";
$dbname = "contoh_checkbox_php";
$conn = mysqli_connect($server, $user, $pass, $dbname);
if(!$conn){
die("Connection Failed: " .mysqli_connect_error());
3. index.php<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Form | Input Data</title>
<style type="text/css">
<!--
.style1 {
font-size: 36px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<table width="45%" border="0" align="center">
<tr>
<td colspan="5" bgcolor="#CCCCCC"><span class="style1">Contoh_Checkbox</span></td>
</tr>
<tr>
<td colspan="5" bgcolor="#CCCCCC"><table width="95%" border="0" align="center">
<tr>
<td colspan="2" bgcolor="#CCCCCC"><div align="center">
<?php
include "config.php";
if(isset($_POST["submit"])) {
$hobi = implode($_POST["hobi"], ', ');
mysqli_query($conn, "INSERT INTO tb_checkbox VALUES ('', '$hobi')");
}
$query = mysqli_query ($conn, "SELECT * FROM tb_checkbox ORDER BY id DESC");
?>
<form action="" method="post">
<div align="left">
<p>
<input type="checkbox" name="hobi[]" value="Mancing">
Mancing
<br>
<input type="checkbox" name="hobi[]" value="Olahraga">
Olahraga
<br>
<input type="checkbox" name="hobi[]" value=" Menyelam">
Menyelam
<br>
<input type="checkbox" name="hobi[]" value="Berpetualangan">
Berpetualangan
<br>
<input type="checkbox" name="hobi[]" value="Membaca">
Membaca<br><br />
<input type="submit" name="submit" value="Simpan">
</p>
</div>
</form>
<table border="1" align="left">
<tr>
<th bgcolor="#FFFFFF">No</th>
<th bgcolor="#FFFFFF">Hobi</th>
<th colspan="2" bgcolor="#FFFFFF">Action</th>
</tr>
<?php $no = 1?>
<?php while ($data = mysqli_fetch_array($query)){?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data['data'] ?></td>
<td><a href="delete.php?delete=<?php echo $data["id"]?>">Hapus</a></td>
<td><a href="update.php?update=<?php echo $data['id']?>">Edit</a></td>
</tr>
<?php } ?>
</table>
<p> </p>
<p> </p>
<p> </p>
</div></td>
</tr>
</table></td>
</tr>
</table><br />
</body>
</html>
}
4. update.php<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Form | Edit</title>
<style type="text/css">
<!--
.style1 {
font-size: 36px;
font-weight: bold;
}
-->
</style>
</head>
<body>
<table width="45%" border="0" align="center">
<tr>
<td colspan="5" bgcolor="#CCCCCC"><span class="style1">Contoh_Checkbox</span></td>
</tr>
<tr>
<td colspan="5" bgcolor="#CCCCCC"><table width="95%" border="0" align="center">
<tr>
<td colspan="2" bgcolor="#CCCCCC"><div align="center">
<?php
include "config.php";
if(isset($_POST["submit"])) {
$id = $_POST['id'];
$hobi = implode(', ', $_POST['hobi']);
$sql = mysqli_query ($conn, "UPDATE tb_checkbox SET data = '$hobi' WHERE id = '$id' ");
header ('location:index.php?sukses');
}
$tampil = $_GET['update'];
$query_tampil = mysqli_query ($conn, "SELECT * FROM tb_checkbox WHERE id = '$tampil' ");
$edit = mysqli_fetch_array($query_tampil);
$checked = explode(', ', $edit['data']);
$query = mysqli_query($conn, "SELECT * FROM tb_checkbox ORDER BY id DESC");
?>
<form action="" method="post">
<div align="left">
<input type="checkbox" name="hobi[]" value="Mancing" <?php in_array ('Mancing', $checked) ? print "checked" : ""; ?> >
Mancing<br>
<input type="checkbox" name="hobi[]" value="Olahraga" <?php in_array ('Olahraga', $checked) ? print "checked" : ""; ?> >
Olahraga<br>
<input type="checkbox" name="hobi[]" value="Menyelam" <?php in_array ('Menyelam', $checked) ? print "checked" : ""; ?> >
Menyelam<br>
<input type="checkbox" name="hobi[]" value="Berpetualangan" <?php in_array ('Berpetualangan', $checked) ? print "checked" : ""; ?> >
Berpetualangan<br>
<input type="checkbox" name="hobi[]" value="Membaca" <?php in_array ('Membaca', $checked) ? print "checked" : ""; ?> >
Membaca<br><br />
<input type="submit" name="submit" value="Simpan">
<input type="hidden" name="id" value="<?php echo $edit["id"]?>"/>
</div>
</form>
<table border="1" align="left">
<tr>
<th bgcolor="#FFFFFF">No</th>
<th bgcolor="#FFFFFF">Hobi</th>
<th colspan="2" bgcolor="#FFFFFF">Action</th>
</tr>
<?php $no = 1?>
<?php while ($data = mysqli_fetch_array($query)){?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data['data'] ?></td>
<td><a href="delete.php?delete=<?php echo $data["id"]?>">Hapus</a></td>
<td><a href="update.php?update=<?php echo $data["id"]?>">Edit</a></td>
</tr>
<?php } ?>
</table>
<p> </p>
<p> </p>
<p> </p>
</div></td>
</tr>
</table></td>
</tr>
</table><br />
</body>
</html>
2. delete.php<?php
include "config.php";
if(isset($_GET["delete"])){
$id = $_GET["delete"];
mysqli_query($conn, "DELETE FROM tb_checkbox WHERE id = '$id'");
header("location:index.php");
}
?>
Sekarang kalian bisa mencoba sistem CRUD checkbox dengan PHP ini.
Jika ada pertanyaan, silahkan tinggalkan komentarnya.by : admin
sama-sama @alan Sau
ReplyDeletePost a Comment
Berkomentarlah yang sopan dan jangan buang waktu untuk melakukan spam. Terimakasih