<?php $mahasiswa = new Mahasiswa(); ?>
<h3 class="page-header">Form Mahasiswa</h3>
<form action="#" id="form_mhs" autocomplete="off">
<input type="hidden" name="txtID"/>
<div class="form-group">
<label>NPM:</label>
<input type="text" name="txtNpm" id="npm" class="form-control" placeholder="NPM" autofocus/>
</div>
<div class="form-group">
<label>NAMA:</label>
<input type="text" name="txtNama" id="nama" class="form-control" placeholder="NAMA"/>
</div>
<button type="button" id="save" class="btn btn-primary btn-sm" onclick="simpan()">Simpan</button>
<button type="button" id="update" class="btn btn-warning btn-sm" disabled="disabled" onclick="ubah()">Ubah</button>
</form>
<h3 class="page-header">Data Mahasiswa</h3>
<table class="table table-striped table-hover">
<thead>
<tr>
<th width="5%">No</th>
<th>NPM</th>
<th>Nama</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$no=(int)1;
foreach ($mahasiswa->data_mahasiswa() as $rows): ?>
<tr>
<td><?php echo $no++ ?></td>
<td><?php echo $rows['npm'] ?></td>
<td><?php echo $rows['nama'] ?></td>
<td>
<a class="btn btn-xs btn-success" href="javascript:void(0)" onclick="ganti(<?php echo $rows['id']?>)">Ubah</a>
<a class="btn btn-xs btn-danger" href="javascript:void(0)" onclick="hapus(<?php echo $rows['id']?>)">Hapus</a>
</td>
</tr>
<?php endforeach?>
</tbody>
</table>
<script>
function simpan() {
var npm = $('#npm').val();
var nama = $('#nama').val();
$.ajax({
url: 'content/mahasiswa/simpan.php',
type: 'POST',
dataType: 'JSON',
data: $('#form_mhs').serialize(),
success:function(data) {
if (!data.success)
{
if (data.errors.NPM) {
alert(data.errors.NPM);
$('#npm').focus();
return false;
}
if (data.errors.NAMA) {
alert(data.errors.NAMA);
$('#nama').focus();
return false;
}
}
else
{
alert(data.message)
window.location.reload();
}
}
})
}
function ganti(id) {
$.ajax({
url: 'content/mahasiswa/tampil_ubah.php',
type: 'POST',
cache:false,
dataType: 'JSON',
cache:false,
data: 'id='+id,
encode:true,
success:function (data) {
console.log(data);
$('#save').attr('disabled', 'disabled');//menonaktifkan tombol simpan
$('#update').removeAttr('disabled');//mengaktifkan tombol ubah
$('input[name="txtID"]').val(data.id);
$('input[name="txtNpm"]').val(data.npm);
$('input[name="txtNama"]').val(data.nama);
}
})
}
function ubah() {
var npm = $('#npm').val();
var nama = $('#nama').val();
$.ajax({
url: 'content/mahasiswa/ubah.php',
type: 'POST',
dataType: 'json',
data: $('#form_mhs').serialize(),
success:function(data) {
if (!data.success)
{
if (data.errors.NPM) {
alert(data.errors.NPM);
$('#npm').focus();
return false;
}
if (data.errors.NAMA) {
alert(data.errors.NAMA);
$('#nama').focus();
return false;
}
}
else
{
alert(data.message)
window.location.reload();
}
}
})
}
function hapus(id) {
if(confirm('Anda Yakin Mau Menghapus?')){
$.ajax({
url: 'content/mahasiswa/hapus.php',
type: 'POST',
dataType: 'JSON',
data: 'id='+id,
encode:true,
success:function (data) {
if(!data.success){
if(data.errors){
alert(data.errors)
}
}else{
alert(data.message);
window.location.reload();
}
}
})
}
}
</script>