Advertisement
cdsatrian

chainned combobox /w javascript

Nov 25th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. <?php
  2. /*
  3. USE test;
  4.  
  5. DROP TABLE IF EXISTS tbl_aspek;
  6. CREATE TABLE IF NOT EXISTS tbl_aspek(
  7.   id_aspek TINYINT(3) UNSIGNED AUTO_INCREMENT,
  8.   aspek VARCHAR(100) NOT NULL,
  9.   prosentase FLOAT NOT NULL,
  10.   PRIMARY KEY(id_aspek)
  11. )ENGINE=MyISAM;
  12.  
  13. INSERT INTO tbl_aspek(id_aspek,aspek,prosentase)
  14. VALUES
  15. (1,'Kecerdasan',20),
  16. (2,'Sikap Kerja',30),
  17. (3,'Perilaku',50);
  18.  
  19. DROP TABLE IF EXISTS tbl_faktor;
  20. CREATE TABLE IF NOT EXISTS tbl_faktor(
  21.   id_faktor SMALLINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  22.   id_aspek TINYINT(3) UNSIGNED NOT NULL,
  23.   faktor VARCHAR(100) NOT NULL,
  24.   nilai TINYINT(3) NOT NULL,
  25.   kelompok SET('core','secondary'),
  26.   PRIMARY KEY(id_faktor)
  27. )ENGINE=MyISAM;
  28.  
  29. INSERT INTO tbl_faktor(id_faktor,id_aspek,faktor,nilai,kelompok)
  30. VALUES
  31. (1,1,'Common Sense',3,'core'),
  32. (2,1,'Verbalisasi Ide',3,'core'),
  33. (3,1,'Sistematika berpikir',4,'secondary'),
  34. (4,1,'Penalaran dan Solusi Real',4,'secondary'),
  35. (5,1,'Konsentrasi',3,'core'),
  36. (6,1,'Logika Praktis',4,'secondary'),
  37. (7,1,'Fleksibilitas Berpikir',4,'secondary'),
  38. (8,1,'Imajinasi Kreatif',5,'core'),
  39. (9,1,'Antisipasi',3,'core'),
  40. (10,1,'Potensi Kecerdasan',4,'secondary'),
  41. (11,2,'Energi Psikis',3,'core'),
  42. (12,2,'Ketelitian dan Tanggung jawab',4,'core'),
  43. (13,2,'Kehati-hatian',2,'secondary'),
  44. (14,2,'Pengendalian Perasaan',3,'secondary'),
  45. (15,2,'Dorongan berprestasi',3,'core'),
  46. (16,2,'Vitalitas Perencanaan',5,'secondary'),
  47. (17,3,'Kekuasaan (Dominance)',3,'core'),
  48. (18,3,'Pengaruh (Influence)',3,'core'),
  49. (19,3,'Keteguhan Hati (Steadiness)',4,'secondary'),
  50. (20,3,'Pemenuhan (Compliance)',5,'secondary');
  51.  
  52. */
  53. $dbhost='localhost';
  54. $dbuser='root';
  55. $dbpass='';
  56. $dbname='test';
  57. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  58. ?>
  59. <!doctype html>
  60. <html>
  61. <head>
  62. <title>Chainned Combobox</title>
  63. <script>
  64.   sub_aspek=new Array();
  65.   <?php
  66.   $sql="SELECT * FROM tbl_faktor";
  67.   $result=$db->query($sql);
  68.   $no=0;
  69.   $aspek=0;
  70.   while($row=$result->fetch_object()){
  71.     if($aspek!=$row->id_aspek){
  72.       $no=0;
  73.       $aspek=$row->id_aspek;
  74.       echo "sub_aspek[{$aspek}]=new Array();\n";
  75.     }
  76.     echo "sub_aspek[{$aspek}][".($no++)."]='{$row->faktor}';\n";
  77.   }
  78.   ?>
  79.   function get_sub_aspek(aspek){
  80.     var x = document.getElementById("id_faktor");
  81.     x.length=0;
  82.     for(var i=0;i<sub_aspek[aspek].length;i++){
  83.       var option = document.createElement("option");
  84.       option.text = sub_aspek[aspek][i];
  85.       x.add(option);
  86.     }
  87.   }
  88. </script>
  89. </head>
  90. <body>
  91. <form method='post'>
  92. <select name='id_aspek' onChange='get_sub_aspek(this.value);'>
  93.   <option>pilih satu</option>
  94. <?php
  95. $sql="SELECT * FROM tbl_aspek";
  96. $result=$db->query($sql);
  97. while($row=$result->fetch_object()){
  98.   echo "<option value='{$row->id_aspek}'>{$row->aspek}</option>";
  99. }
  100. ?>
  101. </select>
  102. <select name='id_faktor' id='id_faktor'>
  103. </select>
  104. <input type='submit'>
  105. </form>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement