<!DOCTYPE html>
<html lang="en">
<head>
<title>Jquery</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/flexslider.css">
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
function Load(data,primid){
$('#loading'+data).empty().append('<img src="img/ajax-loader.gif"/>');
$.post("lokasi.php",{tipe:data,id:primid},
function(result){
$('#cb'+data).empty();
if(result.error==undefined){ //cek ada error atau ngga,klo ga ada lanjut
$('#cb'+data).append('<option>---Pilih Data---</option>');
for(var i=0;i<=result.length;i++){
$('#cb'+data).append($('<option></option>').val(result[i].id).text(result[i].nama));
}
}else{
$('#loading'+data).empty().append('Data tidak ditemukan');
//alert(result.error);
}
},'json'
);
$('#loading'+data).empty();
}
$(function(){
Load('provinsi',0);
$('#cbprovinsi').change(
function(){
if($('#cbprovinsi option:selected').val() !=''){
Load('lokasi',$('#cbprovinsi option:selected').val());
}
}
);
$('#cblokasi').change(
function(){
if($('#cblokasi option:selected').val() !=''){
Load('alamat',$('#cblokasi option:selected').val());
}
}
);
});
</script>
</head>
<body>
<!-- The template uses conditional comments to add wrappers div for ie8 and ie7 - just add .ie or .ie7 prefix to your css selectors when needed -->
<!--[if lt IE 9]><div class="ie"><![endif]-->
<!--[if lt IE 8]><div class="ie7"><![endif]-->
<!-- Header -->
<!-- Server status -->
<header><div class="container_12">
</div></header>
<!-- End server status -->
<!-- Main nav -->
<form class="block-content form" action="" method="post" id = "form_admin" name = "form_admin">
<h1>Location Management <a href=""><img src="images/icons/fugue/navigation-180.png" width="16" height="16"> Back</a></h1>
<div class="block-header">Register Location</div>
<div class="columns">
<div class="colx2-left">
<fieldset>
<legend>Profile</legend>
<table cellpadding="3" class="table-padding">
<tr title="Required">
<td><b>Provinsi</b><span class="required">*</span></td>
<td>:</td>
<td><select id="cbprovinsi"></select></td>
</tr>
<tr title="Required">
<td><b>Location</b><span class="required">*</span></td>
<td>:</td>
<td><select id="cblokasi"></select></td>
</tr>
<tr title="Required">
<td><b>Alamat</b><span class="required">*</span></td>
<td>:</td>
<td><select id="cbalamat"></select></td>
</tr>
</table>
</fieldset>
</div>
</div>
<div class="block-footer" style="height:30px;">
<div class="float-right">
<button type="submit"><img src="images/icons/fugue/tick-octagon.png" name = "save" id ="save" onclick="saveForm(); return false;" width="16" height="16"> Save</button>
<button type="reset" class="grey" onclick="this.form.reset();"><img src="images/icons/fugue/arrow-circle.png" width="16" height="16"> Reset</button>
<button type="reset" class="red" onclick="document.location.href='http:register.php';"><img src="images/icons/fugue/cross-white.png" width="16" height="16"> Cancel</button>
</div>
</div>
</form>
<br>
<p>
<span class="required">*</span> Required</p>
</div></div>
</section>
<div class="clear"></div>
</body>
</html>
<?php
$conn=mysql_connect('localhost','root','');
mysql_select_db('',$conn);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$tipedata=$_POST['tipe'];
$id=$_POST['id'];
switch($tipedata){
case 'alamat':
$sql='SELECT kode,alamat FROM '.$tipedata.' WHERE kode_lokasi = "'.$id.'" ORDER BY alamat ASC';break;
case 'lokasi':
$sql='SELECT kode,lokasi FROM '.$tipedata.' WHERE kode_provinsi = "'.$id.'" ORDER BY lokasi ASC';break;
case 'provinsi':
$sql='SELECT kode,provinsi FROM '.$tipedata.' ORDER BY provinsi ASC';break;
}
$res = array(); //reson variable yang akan digunakan
/*if ($tipedata != 'provinsi'){
$res['error']='error : '.$sql;
die(json_encode($res));
}*/
$kueri=mysql_query($sql);
if($kueri){
if(mysql_num_rows($kueri) > 0){
while($row=mysql_fetch_object($kueri)){
$res[]=$row;
}
}else{
$res['error']='Data tidak ditemukan';
}
}else{
$res['error']='error server : '.mysql_error();
}
}
die(json_encode($res));
?>
<?php
$conn=mysql_connect('localhost','root','');
mysql_select_db('',$conn);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$tipedata=$_POST['tipe'];
$id=$_POST['id'];
switch($tipedata){
case 'alamat':
$sql='SELECT kode,alamat FROM '.$tipedata.' WHERE kode_lokasi = "'.$id.'" ORDER BY alamat ASC';break;
case 'lokasi':
$sql='SELECT kode,lokasi FROM '.$tipedata.' WHERE kode_provinsi = "'.$id.'" ORDER BY lokasi ASC';break;
case 'provinsi':
$sql='SELECT kode,provinsi FROM '.$tipedata.' ORDER BY provinsi ASC';break;
}
$kueri=mysql_query($sql);
echo '<option value="">--- pilih ---</option>';
while($row=mysql_fetch_object($kueri)){
echo '<option value="'.$row->kode.'">';
switch($tipedata){
case 'alamat':
echo $row->alamat;break;
case 'lokasi':
echo $row->lokasi;break;
case 'provinsi':
echo $row->provinsi;break;
}
echo '</option>';
}
}
?>
dan ubah fungsi Load() menjadifunction Load(data,primid){
$('#loading'+data).empty().append('<img src="img/ajax-loader.gif"/>');
$.post("lokasi.php",{tipe:data,id:primid},
function(result){
$('#cb'+data).html(result);
$('#loading'+data).empty();
}
);
}
<?php
$conn=mysql_connect('localhost','root','');
mysql_select_db('',$conn);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$tipedata=$_POST['tipe'];
$id=$_POST['id'];
switch($tipedata){
case 'alamat':
$sql='SELECT kode_alamat,alamat FROM '.$tipedata.' WHERE id_lokasi = "'.$id.'" ORDER BY alamat ASC';break;
case 'lokasi':
$sql='SELECT kode_lokasi,lokasi FROM '.$tipedata.' WHERE id_provinsi = "'.$id.'" ORDER BY lokasi ASC';break;
case 'provinsi':
$sql='SELECT kode,provinsi FROM '.$tipedata.' ORDER BY provinsi ASC';break;
}
$kueri=mysql_query($sql);
echo '<option value="">--- Select ---</option>';
while($row=mysql_fetch_object($kueri)){
echo '<option value="'.$row->kode.'">';
switch($tipedata){
case 'alamat':
echo $row->alamat;break;
case 'lokasi':
echo $row->lokasi;break;
case 'provinsi':
echo $row->provinsi;break;
}
echo '</option>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Jquery</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/flexslider.css">
<script language="javascript" src="jquery.js"></script>
<script language="javascript">
function Load(data,primid){
$('#loading'+data).empty().append('<img src="img/ajax-loader.gif"/>');
$.post("lokasi.php",{tipe:data,id:primid},
function(result){
$('#cb'+data).html(result);
$('#loading'+data).empty();
}
);
}
$(function(){
Load('provinsi',0);
$('#cbprovinsi').change(
function(){
if($('#cbprovinsi option:selected').val() !=''){
Load('lokasi',$('#cbprovinsi option:selected').val());
}
}
);
$('#cblokasi').change(
function(){
if($('#cblokasi option:selected').val() !=''){
Load('alamat',$('#cblokasi option:selected').val());
}
}
);
});
</script>
</head>
<body>
<!-- The template uses conditional comments to add wrappers div for ie8 and ie7 - just add .ie or .ie7 prefix to your css selectors when needed -->
<!--[if lt IE 9]><div class="ie"><![endif]-->
<!--[if lt IE 8]><div class="ie7"><![endif]-->
<!-- Header -->
<!-- Server status -->
<header><div class="container_12">
</div></header>
<!-- End server status -->
<!-- Main nav -->
<form class="block-content form" action="" method="post" id = "form_jquery.php" name = "form_admin">
<h1>Location Management <a href=""><img src="images/icons/fugue/navigation-180.png" width="16" height="16"> Back</a></h1>
<div class="block-header">Register Location</div>
<div class="columns">
<div class="colx2-left">
<fieldset>
<legend>Profile</legend>
<table cellpadding="3" class="table-padding">
<tr title="Required">
<td><b>Kode</b><span class="required">*</span></td>
<td>:</td>
<td><input type="text" name="kode" validation="isNotEmpty" title="Full name" style="width:300px" value="" /></td>
</tr>
<tr title="Required">
<td><b>Provinsi</b><span class="required">*</span></td>
<td>:</td>
<td><select id="cbprovinsi"></select></td>
</tr>
<tr title="Required">
<td><b>Location</b><span class="required">*</span></td>
<td>:</td>
<td><select id="cblokasi"></select></td>
</tr>
<tr title="Required">
<td><b>Alamat</b><span class="required">*</span></td>
<td>:</td>
<td><select id="cbalamat"></select></td>
</tr>
</table>
</fieldset>
</div>
</div>
<div class="block-footer" style="height:30px;">
<div class="float-right">
<button type="submit"><img src="images/icons/fugue/tick-octagon.png" name = "save" id ="save" onclick="saveForm(); return false;" width="16" height="16"> Save</button>
<button type="reset" class="grey" onclick="this.form.reset();"><img src="images/icons/fugue/arrow-circle.png" width="16" height="16"> Reset</button>
<button type="reset" class="red" onclick="document.location.href='http:register.php';"><img src="images/icons/fugue/cross-white.png" width="16" height="16"> Cancel</button>
</div>
</div>
</form>
<br>
<p>
<span class="required">*</span> Required</p>
</div></div>
</section>
<div class="clear"></div>
</body>
</html>