Advertisement
cdsatrian

Latihan AJaX #2

Feb 9th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1. //--FILE: latihan2.php
  2. <!DOCTYPE html>
  3. <html>
  4.     <head>
  5.         <title>Latihan AJaX</title>
  6.     </head>
  7.     <body>
  8.         <form>
  9.             Kota
  10.             <select id='kota' name='kota'>
  11.                 <option value=''>--pilih kota --</option>
  12.                 <?php
  13.                 $kota=array('1'=>'Jakarta','Bandung','Semarang','Yogyakarta','Surabaya','Denpasar');
  14.                 foreach($kota as $val=>$text) echo "<option value='{$val}'>{$text}</option>";
  15.                 ?>
  16.             </select><br>
  17.             bioskop <select id='bioskop' name='bioskop'><option value=''>--pilih bioskop --</option></select><br>
  18.             studio <select id='studio' name='studio'><option value=''>--pilih studio --</option></select><br>
  19.             <input type='submit' value='kirim'>
  20.         </form>
  21.     </body>
  22.     <script
  23.         src="https://code.jquery.com/jquery-1.12.4.min.js"
  24.         integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
  25.         crossorigin="anonymous">
  26.     </script>
  27.     <script>
  28.     $(document).ready(function(){
  29.         $('#kota').change(function(){
  30.             $.post(
  31.                 'bioskop_ajax.php',
  32.                 {'kota':$(this).val()},
  33.                 function(data){
  34.                     $('#bioskop').html(data);
  35.                 }
  36.             );
  37.         });
  38.         $('#bioskop').change(function(){
  39.             $.post(
  40.                 'studio_ajax.php',
  41.                 {'bioskop':$(this).val()},
  42.                 function(data){
  43.                     $('#studio').html(data);
  44.                 }
  45.             );
  46.         });
  47.     });
  48.     </script>
  49. </html>
  50.  
  51. //-- FILE : bioskop_ajax.php
  52. <?php
  53. if(isset($_POST['kota'])){
  54.     $data=array(
  55.         '1'=>array('1.1'=>'Gading IMAX','1.2'=>'Gandaria IMAX','1.3'=>'Gading PREMIERE'),
  56.         '2'=>array('2.1'=>'Ciwalk PREMIERE','2.2'=>'Braga XXI'),
  57.         '3'=>array('3.1'=>'Citra XXI','3.2'=>'Paragon XXI'),
  58.         '4'=>array('4.1'=>'Ambarukmo PREMIERE','4.2'=>'Jogja City PREMIERE'),
  59.         '5'=>array('5.1'=>'Tunjungan 5 IMAX','5.2'=>'Grand City PREMIERE'),
  60.         '6'=>array('6.1'=>'Beachwalk PREMIERE','6.2'=>'Level21 PREMIERE'),
  61.     );
  62.     $hasil="<option value=''>--pilih bioskop--</option>";
  63.     foreach($data[$_POST['kota']] as $id=>$bioskop) $hasil.="<option value='{$id}'>{$bioskop}</option>";
  64.     echo $hasil;
  65. }
  66.  
  67. //-- FILE : studio_ajax.php
  68. <?php
  69. if(isset($_POST['bioskop'])){
  70.     $data=array(
  71.         '1.1'=>array('1.1.1'=>'first','1.1.2'=>'second'),
  72.         '1.2'=>array('1.2.1'=>'first','1.2.2'=>'second','1.2.3'=>'third'),
  73.         '1.3'=>array('1.3.1'=>'first','1.3.2'=>'second'),
  74.         '2.1'=>array('2.1.1'=>'first','2.1.2'=>'second'),
  75.         '2.2'=>array('2.2.1'=>'first','2.2.2'=>'second'),
  76.         '3.1'=>array('3.1.1'=>'first','3.1.2'=>'second','3.1.3'=>'third'),
  77.         '3.2'=>array('3.2.1'=>'first','3.2.2'=>'second'),
  78.         '4.1'=>array('4.1.1'=>'first','4.1.2'=>'second','4.1.3'=>'third'),
  79.         '4.2'=>array('4.2.1'=>'first','4.2.2'=>'second','4.2.3'=>'third'),
  80.         '5.1'=>array('5.1.1'=>'first','5.1.2'=>'second'),
  81.         '5.2'=>array('5.2.1'=>'first','5.2.2'=>'second'),
  82.     );
  83.     $hasil="<option value=''>--pilih studio--</option>";
  84.     foreach($data[$_POST['bioskop']] as $id=>$studio) $hasil.="<option value='{$id}'>{$studio}</option>";
  85.     echo $hasil;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement