Read 593 times | Created 2013-01-17 17:47:22 | Updated 2014-05-17 18:01:37 | | |

 

//-------file 'siswa.php'

<?php
/*********************************
FILENAME  : siswa.php
CREATE BY  : cahya dsn
PURPOSE   : pilih data siswa berdasar id
CREATE DATE : 2013-01-18
**********************************
#table creation

USE `test`;

DROP TABLE IF EXISTS `t_siswa`;
CREATE TABLE IF NOT EXISTS `t_siswa` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table t_siswa',
 `nama` varchar(30) NOT NULL COMMENT 'nama siswa',
 `alamat` varchar(50) NOT NULL COMMENT 'alamat siswa',
 PRIMARY KEY(`id`)
) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data siswa';

INSERT INTO `t_siswa`(`id`,`nama`,`alamat`) VALUES
(NULL,'Agus','Bandung'),
(NULL,'Budi','Jakarta'),
(NULL,'Cici','Bogor'),
(NULL,'Dewi','Tangerang');
*/

$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="test";
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$sql="SELECT id FROM t_siswa";
$result=$db->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>Pilih Siswa</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<form method="post">
<table>
<tr>
  <td>ID</td>
  <td>
    <select id="pilih">
      <option value=''>pilih salah satu</option>
<?php
      while($row=$result->fetch_object()){
        echo "<option value='{$row->id}'>{$row->id}</option>n";
      }
?>
    </select>
  </td>
</tr>
<tr>
  <td>Nama</td>
  <td><input type="text" id="nama" readonly="readonly" /></td>
</tr>
<tr>
  <td>Alamat</td>
  <td><input type="text" id="alamat" readonly="readonly" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
$(function(){
  $('#pilih').change(function(){
    $.post("proses.php",{"id":$('#pilih').val()},
      function(data){
        $('#nama').val(data.nama);
        $('#alamat').val(data.alamat);
      },"json");
  });
});
</script>
</body>
</html>

//------------file 'proses.php'

<?php
/*********************************
FILENAME  : proses.php
CREATE BY  : cahya dsn
PURPOSE   : proses pilih data siswa berdasar id
CREATE DATE : 2013-01-18
**********************************/

$id=isset($_POST['id'])?$_POST['id']:'';
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="test";
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$sql="SELECT nama,alamat FROM t_siswa WHERE id='".$id."'";
$result=$db->query($sql);
$row=$result->fetch_object();
$hasil=array("nama"=>$row->nama,"alamat"=>$row->alamat);
echo json_encode($hasil);
?>