Advertisement
cdsatrian

detail without ajax

Sep 22nd, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.29 KB | None | 0 0
  1. <?php
  2. /*********************************
  3. FILENAME   : detail.php
  4. CREATE BY  : CAHYA DSN
  5. CREATE DATE: 2013-09-23
  6. NOTES      : native javascript, client side only
  7. **********************************
  8. USE test;
  9.  
  10. DROP TABLE IF EXISTS `tbl_test`;
  11. CREATE TABLE IF NOT EXISTS `tbl_test` (
  12.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  13.   `nama` VARCHAR(25)NOT NULL,
  14.   `alamat` VARCHAR(100) NOT NULL,
  15.   PRIMARY KEY (`id`)
  16. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  17.  
  18. INSERT INTO tbl_test(`id`,`nama`,`alamat`)
  19. VALUES
  20. (1,'Adam','Hogwart'),
  21. (2,'Budi','Black Lake'),
  22. (3,'Carlie','Forbidden Forest'),
  23. (4,'Dona','Diagon Alley');
  24. **********************************
  25. //--- DATABASE CONFIGURATION
  26. $dbhost='localhost';
  27. $dbuser='root';
  28. $dbpass='';
  29. $dbname='test';
  30. //--- DATABASE CONNNECTION (MYSQLI)
  31. $db=new mysqli ($dbhost,$dbuser,$dbpass,$dbname);
  32. $sql="SELECT * FROM tbl_test"
  33. $data=array();
  34. if($result=$db->query($sql))
  35. {
  36.   while($record=$result->fetch_object())
  37.   {
  38.     $data[]=array($record->id,$record->nama,$record->alamat);
  39.   }
  40.   $result->close();
  41. }
  42. */
  43. ?>
  44. <!DOCTYPE html>
  45. <html>
  46.   <head>
  47.     <script>
  48.       function detail(no){
  49.         document.getElementById('detail').style.display='block';
  50.         document.getElementById('no').value=no;
  51.         document.getElementById('id').value=document.getElementById('id'+no).value;
  52.         document.getElementById('nama').value=document.getElementById('nama'+no).value;
  53.         document.getElementById('alamat').value=document.getElementById('alamat'+no).value;
  54.       }
  55.       function batal(){
  56.         document.getElementById('detail').style.display='none';
  57.       }
  58.       function simpan(){
  59.         var no=document.getElementById('no').value
  60.         document.getElementById('id'+no).value=document.getElementById('id').value;
  61.         document.getElementById('nama'+no).value=document.getElementById('nama').value;
  62.         document.getElementById('alamat'+no).value=document.getElementById('alamat').value;      
  63.         document.getElementById('detail').style.display='none';
  64.       }
  65.     </script>
  66.   </head>
  67.   <body>
  68.     <table>
  69.       <tr>
  70.         <th>ID</th>
  71.         <th>Nama</th>
  72.         <th>Alamat</th>
  73.       </tr>
  74.       <?php
  75.       $i=0;
  76.       foreach($data as $dt){
  77.         echo "<tr>"
  78.             ."<td><input type='text' id='id".++$i."' value='{$dt[0]}' readonly='readonly'/></td>"
  79.             ."<td><input type='text' id='nama$i' value='{$dt[1]}' readonly='readonly'/></td>"
  80.             ."<td><input type='text' id='alamat$i' value='{$dt[2]}' readonly='readonly'/></td>"
  81.             ."<td><input type='button' onclick='detail($i)' value='detail'/></td>"
  82.             ."</tr>";
  83.       }
  84.       ?>
  85.     </table>
  86.     <div id="detail" style='display:none'>
  87.       <input type="hidden" id='no' name="no"/>
  88.       <table>
  89.         <tr><td>ID</td><td><input type="text" id='id' name="id" readonly="readonly"/></td></tr>
  90.         <tr><td>Nama</td><td><input type="text" id='nama' name="nama"/></td></tr>
  91.         <tr><td>Alamat</td><td><input type="text" id='alamat' name="alamat"/></td></tr>
  92.         <tr>
  93.           <td colspan="2" align="right">
  94.             <input type="button" value="simpan" onclick="simpan()"/>
  95.             <input type="button" value="batal" onclick="batal()"/>
  96.           </td>
  97.         </tr>
  98.     </div>
  99.   </body>
  100. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement