Tutorial Combo Box Dinamis PHP + Mysql

Tools yang di butuhkan = xampp

Buatlah Database dengan nama kendaraan lalu, Buatlah 2 Table dengan nama merk_mobil dan
namamobil

Struktur Table merk_mobil



Struktur Table namamobil







inputlah data merkmobil pada table merk_mobil sesuai pada list di bawah ini di phpmyadmin
(di mysql langsung pun bisa)











lalu, Buatlah Struktur file (di dalam htdocs) seperti gambar di bawah ini



Setelah itu kita mulai proses coding
koneksi.php
<?php
$host = "localhost";
$username = "root";
$password ="";
$database = "kendaraan";

$connection = mysql_connect($host,$username,$password) or die("no database : ".mysql_error());
$database = mysql_select_db($database);
?>
form.css
.input {
    width: 100%;
    padding: 12px 20px;
    margin: 8px 0;
    display: inline-block;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
}

.button {
    width: 20%;
    background-color: #4CAF50;
    color: white;
    padding: 10px 0px;
    margin: 8px 0;
    border: none;
    border-radius: 1px;
    cursor: pointer;
    font-size: 15px;
}

.button:hover {
    background-color: #45a049;
}

div {
    border-radius: 5px;
    background-color: #f2f2f2;    padding: 20px;
}

.center {
    margin: auto;
    width: 65%;
    border: 10px;
    padding: 40px;
}
/* solid #73AD21; */
.centers {
    margin: auto;
    width: 65%;
}

input.php
<html>

<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title>  </title>
<body>

<h2 class="centers"> <center> Contoh Combo Box Dinamis PHP + Mysql </center> </h2>
<br>
<div class="center">
<form action="prosesinput.php" method="post">

<select name="merk_mobil" class="input" required="required">
<option disabled="disabled" selected="selected"> Pilih Merk Mobil </option>
<?php
include "koneksi.php";

$query = "select * from merk_mobil";
$result = mysql_query($query,$connection);
while($row = mysql_fetch_array($result))
{
?>
<option> <?php echo $row['merkmobil'];?> </option>
<?php
}
?>
</select>

<label> Masukkan Nama Mobil </label>
<input type="text" name="namamobil" required="required" class="input">

<input type="submit" value="Submit" name="submit" class="button">
<input type="reset" value="reset" class="button">
  </form>
</body>
<!--
<script type="text/javascript">
    function bukatab() {
        window.open("registrasi.php");
    }
-->
</script>
</head>
</html>

prosesinput.php
<?php
if(isset($_POST['submit']))
{
session_start();
include "koneksi.php";

$merk_mobil = $_POST['merk_mobil'];
$namamobil = $_POST['namamobil'];

$query = "insert into namamobil (id,merkmobil,namamobil) values ('','$merk_mobil','$namamobil')";
$result = mysql_query($query,$connection) or die("Query salah : ".mysql_error());

if($result)
{
echo "Input Berhasil";
?>
<meta http-equiv ='refresh' content= '0.3; url=input.php'>
<?php
}
}


?>

Hasil









combo box terbaca pada tabel namamobil











Download Source Code + Database

1 Response to "Tutorial Combo Box Dinamis PHP + Mysql"