Advertisement
kuchuz

Tugas 7 PWEB-D tugas2.php

Dec 31st, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <title>Tugas PWEB - AJAX</title>
  4.  
  5. <script>
  6.     function loadProvinsi() {
  7.         const xhttp = new XMLHttpRequest();
  8.         xhttp.onload = function () {
  9.             const myObj = JSON.parse(this.responseText);
  10.             let html = "<option>Pilih Provinsi</option>";
  11.             let idx = 0;
  12.             for (let x of Object.keys(myObj)) {
  13.                 html += "<option";
  14.                 html += ` value='${idx}'>`;
  15.                 html += x;
  16.                 html += "</option>";
  17.                 idx += 1;
  18.             }
  19.             document.getElementById("provinsi").innerHTML = html;
  20.         }
  21.         xhttp.open("GET", "tugas2.json", true);
  22.         xhttp.send();
  23.     }
  24.  
  25.     function loadKabKota(idx) {
  26.         const xhttp = new XMLHttpRequest();
  27.         xhttp.onload = function () {
  28.             const myObj = JSON.parse(this.responseText);
  29.             let html = "<option>Pilih Kabupaten/Kota</option>";
  30.             for (let x of Object.values(myObj)[idx]) {
  31.                 html += "<option>";
  32.                 html += x;
  33.                 html += "</option>";
  34.             }
  35.             document.getElementById("kabkota").innerHTML = html;
  36.         }
  37.         xhttp.open("GET", "tugas2.json", true);
  38.         xhttp.send();
  39.     }
  40. </script>
  41.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  42.     <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  43. </head>
  44. <body onload="loadProvinsi()">
  45.     <center>
  46.         <h1>Pilih Alamat</h1>
  47.         <br/>
  48.         <br/>
  49.         <select id="provinsi" class="form-select mb-3" onchange="loadKabKota(document.getElementById('provinsi').value)"><option>Pilih Provinsi</option></select>
  50.         <br/>
  51.         <select id="kabkota" class="form-select"><option>Pilih Kabupaten/Kota</option></select>
  52.         <br/>
  53.         <br/>
  54.         <br/>
  55.         <br/>
  56.         <a href="index.php" class="btn btn-primary">Menuju Tugas ke 1</a>
  57.     </center>
  58. </body>
  59. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement