php-crud-oop/form.php

149 lines
4.3 KiB
PHP
Raw Normal View History

2020-01-02 23:24:47 +07:00
<!doctype html>
<?php
include('database.php');
include('mahasiswa.php');
$mhs = new Mahasiswa();
$mhs->connect();
if(!isset($_POST['submit'])){
if (isset($_GET['id'])){
$car = $_GET['id'];
$action='update&id='.$_GET['id'].'';
$mhs->select('mahasiswa','*',"nim='$car'");
$res = $mhs->getResult();
}else{
$action='insert';
}
}
else{
if ($_GET['action']=='update'){
$id = $_GET['id'];
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
if(!empty($_FILES['file']['name'])){
$photos = $mhs->foto($_FILES,$id,'update',$nim);
$foto = $foto['lokasi'];
}
else{
$foto = $mhs->renamefoto($id,$nim);
}
$rows=array(
'nim' => $nim,
'nama' => $nama,
'telp' => $telp,
'asal' => $asal,
'alamat'=> $alamat,
'foto' => $foto
);
if($mhs->update('mahasiswa',$rows,'nim = '.$_GET['id'].'')){
header('Location: index.php');
}else{
echo 'Tidak bisa input';
}
}
else{
$nim = $_POST['nim'];
$nama = $_POST['nama'];
$telp = $_POST['telp'];
$asal = $_POST['asal'];
$alamat = $_POST['alamat'];
if(!empty($_FILES['file']['name'])){
$foto = $mhs->foto($_FILES,$nim,'insert',$nim);
$lokasi = $foto['lokasi'];
$values=array($nim,$nama,$telp,$asal,$alamat,$lokasi);
$rows = null;
}else{
$values=array($nim,$nama,$telp,$asal,$alamat);
$rows= "nim,nama,telp,asal,alamat";
}
if($mhs->insert('mahasiswa',$values,$rows)){
header('Location: index.php');
}else{
echo 'Tidak bisa input';
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link href="style.css" rel="stylesheet">
<link href="bootstrap.min.css" rel="stylesheet">
<style type="text/css">
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Registrasi</h1>
</div>
<form method="post" action="?action=<?php echo $action ?>" enctype="multipart/form-data">
<fieldset>
<legend>Data</legend>
<table>
<tr>
<td width="300px">Nama</td>
<td width="30px">:</td>
<td><input type="text" name="nama" value="<?php if (isset($_GET['id'])) echo $res['nama']; ?>" maxlength="30" required></td>
</tr>
<tr>
<td>NIM</td>
<td>:</td>
<td><input type="text" name="nim" value="<?php if (isset($_GET['id'])) echo $res['nim']; ?>" onkeypress="return isNumberKey(event)" maxlength="12" required></td>
</tr>
<tr>
<td>Alamat</td>
<td>:</td>
<td><input type="text" name="alamat" value="<?php if (isset($_GET['id'])) echo $res['alamat']; ?>" maxlength="50" required></td>
</tr>
<tr>
<td>Telp</td>
<td>:</td>
<td><input type="text" name="telp" value="<?php if (isset($_GET['id'])) echo $res['telp']; ?>" onkeypress="return isNumberKey(event)" maxlength="20" required></td>
</tr>
<tr>
<td>Sekolah Asal</td>
<td>:</td>
<td><input type="text" name="asal" value="<?php if (isset($_GET['id'])) echo $res['asal']; ?>" maxlength="20" required></td>
</tr>
<tr>
<td>Photo</td>
<td>:</td>
<td style="text-align:center">
<?php if (!empty($res['foto'])){
?>
<div style="background-color:#f0f0f0;padding:10px;">
<img src="<?php echo $res['foto'] ?>" height="200" width="300" name="foto"></img></br>
</div>
<?php
}
?>
<br/><input type="file" name="file" id="inputfile" style>
</td>
</tr>
</table>
</fieldset>
<div style="text-align:center;margin:20px;">
<a href="index.php"><button type="button" class="btn"><i class=" icon-ban-circle"></i> Cancel</button></a>
<button type="submit" name="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> Save</button>
</div>
</form>
</div>
</body>
</html>
<script>
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
{
alert('Number Only');
return false;
}
return true;
};
</script>