Skip to content

Instantly share code, notes, and snippets.

@ruliarmando
Created March 14, 2014 02:51
Show Gist options
  • Select an option

  • Save ruliarmando/9541337 to your computer and use it in GitHub Desktop.

Select an option

Save ruliarmando/9541337 to your computer and use it in GitHub Desktop.
ajax-combo
<?php
$data = array(
'hendra' => 78,
'nina' => 90,
'budi' => 80,
'chandra' => 77,
'anisa' => 92,
'dedi' => 90,
);
if(isset($_POST['nama']))
{
$nama = $_POST['nama'];
if(isset($data[$nama]))
{
echo $data[$nama];
exit();
}
}
?>
<!doctype html>
<html>
<head>
<title>Testing Ajax ComboBox</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('#select_nama').change(function(){
$.ajax({
url: window.location.pathname,
type: 'post',
data: {'nama' : $(this).val()},
dataType: 'text',
success: function(data){
$('#txt_nilai').val(data);
}
});
});
});
</script>
</head>
<body>
<select name="nama" id="select_nama">
<option value="">--pilih--</option>
<?php foreach($data as $k => $v): ?>
<option value="<?php echo $k; ?>"><?php echo $k; ?></option>
<?php endforeach; ?>
</select><br />
<input type="text" name="nilai" id="txt_nilai" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment