Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ----CONTOH DATA
- DROP TABLE IF EXISTS fakultas;
- CREATE TABLE IF NOT EXISTS fakultas(
- id INT AUTO_INCREMENT PRIMARY KEY,
- nama VARCHAR(50)
- );
- INSERT INTO fakultas(nama)
- VALUES
- ('Tehnik'),
- ('Ilmu Komputer'),
- ('Ekonomi');
- DROP TABLE IF EXISTS jurusan;
- CREATE TABLE IF NOT EXISTS jurusan(
- id INT AUTO_INCREMENT PRIMARY KEY,
- id_fakultas INT,
- nama VARCHAR(50)
- );
- INSERT INTO jurusan(id_fakultas,nama)
- VALUES
- (1,'Elektro'),
- (1,'Mesin'),
- (1,'Sipil'),
- (2,'Sistem Informatika'),
- (2,'Tehnik Informatika'),
- (3,'Akuntansi'),
- (3,'Manajemen');
- DROP TABLE IF EXISTS konsentrasi;
- CREATE TABLE IF NOT EXISTS konsentrasi(
- id INT AUTO_INCREMENT PRIMARY KEY,
- id_jurusan INT,
- nama VARCHAR(50)
- );
- INSERT INTO konsentrasi(id_jurusan,nama)
- VALUES
- (1,'Arus Kuat'),
- (1,'Instrumentasi'),
- (1,'Telekomunikasi'),
- (2,'Industri'),
- (2,'Kendaraan'),
- (3,'Jalan Raya'),
- (3,'Pengairan'),
- (4,'Sistem Informasi Korporat'),
- (4,'E-Business'),
- (5,'Sistem Kecerdasan'),
- (5,'Sistem Perangkat Lunak'),
- (6,'Keuangan'),
- (6,'Auditing'),
- (6,'Manajerial'),
- (7,'Pemasaran'),
- (7,'Sumber Daya Manusia');
- */
- //-- FILE : koneksi.php
- <?php
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- //-- FILE : fakultas.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>Latihan AJaX</title>
- </head>
- <body>
- <form>
- Fakultas
- <select id='fakultas' name='fakultas'>
- <option value=''>--pilih fakultas --</option>
- <?php
- include 'koneksi.php';
- $sql='SELECT id,nama FROM fakultas ORDER BY nama';
- $result=$db->query($sql);
- while($row=$result->fetch_object()){
- echo "<option value='{$row->id}'>{$row->nama}</option>";
- }
- ?>
- </select>
- jurusan <select id='jurusan' name='jurusan'><option value=''>--pilih jurusan --</option></select>
- konsentrasi <select id='konsentrasi' name='konsentrasi'><option value=''>--pilih konsentrasi --</option></select><br>
- <input type='submit' value='kirim'>
- </form>
- </body>
- <script
- src="https://code.jquery.com/jquery-1.12.4.min.js"
- integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
- crossorigin="anonymous">
- </script>
- <script>
- $(document).ready(function(){
- $('#fakultas').change(function(){
- $.post(
- 'jurusan_ajax.php',
- {'fakultas':$(this).val()},
- function(data){
- $('#jurusan').html(data);
- }
- );
- });
- $('#jurusan').change(function(){
- $.post(
- 'konsentrasi_ajax.php',
- {'jurusan':$(this).val()},
- function(data){
- $('#konsentrasi').html(data);
- }
- );
- });
- });
- </script>
- </html>
- //-- FILE : jurusan_ajax.php
- <?php
- if(isset($_POST['fakultas'])){
- $hasil="<option value=''>--pilih jurusan--</option>";
- include 'koneksi.php';
- $sql="SELECT id,nama FROM jurusan WHERE id_fakultas={$_POST['fakultas']} ORDER BY nama";
- $result=$db->query($sql);
- while($row=$result->fetch_object()){
- $hasil.="<option value='{$row->id}'>{$row->nama}</option>";
- }
- echo $hasil;
- }
- //-- FILE : konsentrasi_ajax.php
- <?php
- if(isset($_POST['jurusan'])){
- $hasil="<option value=''>--pilih konsentrasi--</option>";
- include 'koneksi.php';
- $sql="SELECT id,nama FROM konsentrasi WHERE id_jurusan={$_POST['jurusan']} ORDER BY nama";
- $result=$db->query($sql);
- while($row=$result->fetch_object()){
- $hasil.="<option value='{$row->id}'>{$row->nama}</option>";
- }
- echo $hasil;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement