Now I want to share about how to create dynamic combobox with javascript and PHP version of me. Basically a PHP script utilizes cookies to query the database. Because I have not been able to create a web-based ajax, so I manipulate the javascript function combined with PHP.
First we create the database tables in advance
CREATE TABLE `barang`.`barang` ( `id` VARCHAR( 20 ) NOT NULL , `nama` VARCHAR( 20 ) NOT NULL , `merk` VARCHAR( 20 ) NOT NULL , `harga` INT( 11 ) NOT NULL , PRIMARY KEY ( `id` ) );For example I input the following data
INSERT INTO `barang` VALUES ('b01', 'Kaos', 'Merk01', '5000'); INSERT INTO `barang` VALUES ('b02', 'Kemeja', 'Merk02', '10000'); INSERT INTO `barang` VALUES ('b03', 'Celana', 'Merk03', '15000');
then we create a PHP file
konek.php
//host database $dbhost = "nama_hos"; //User Database $dbuser = "username"; //Password Database $dbpswd = "password"; //Database name $dbname = "nama_database"; mysql_connect($dbhost, $dbuser, $dbpswd); mysql_select_db($dbname);
combobox.php
include 'konek.php'; echo" "; $cookie = $_COOKIE['barang']; $query = mysql_query("SELECT * FROM barang ORDER BY id"); echo""; $query_barang = mysql_query("SELECT * FROM barang WHERE id = '".$cookie."'"); $rows = mysql_num_rows($query_barang); $data_barang = mysql_fetch_array($query_barang); $harga = number_format($data_barang['harga'], 0, ',', '.'); if($rows>0){ echo"
".$data_barang['nama']." | ".$data_barang['merk']." | Rp. ".$harga." |
Silahkan Pilih Nama Barang |