<!DOCTYPE html>
<html>
<head>
	<title>New Users</title>
<link rel="stylesheet" type="text/css" href="css/userprofile.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/simple-keyboard.css">
<script src="js/jquery-2.2.3.min.js"></script>
<script type="text/javascript">
function executeQuery() {
  $.ajax({
    url: 'action.php',
    success: function(data) {
		// console.log($(location).attr('search'));
		if($(location).attr('search') !== '') {
			window.location.href = $(location).attr('pathname');
		}
		$("#rfid_code").val(data);
    }
  });
}
// setInterval should fit your situation.
var timeout = setInterval(function() {
	if (!$("#rfid_code").val()) {
		executeQuery();
	} else {
		clearInterval(timeout);
	}
}, 2000);
</script>
</head>
<body>

<?php 
	require_once('header.php');
	require_once("connectDB.php");

	if (isset($_GET['simpan']) && $_GET['simpan'] == 'ok') {
		echo "<script> alert('Pendaftaran Berhasil !');</script>";
	}

	if (isset($_GET['action']) && $_GET['action'] == 'save') {
		$nama = $_POST['nama'];
		$nik = $_POST['nik'];
		$jabatan = $_POST['jabatan'];
		$rfid_code = $_POST['rfid_code'];
	
		$qryInput = "INSERT INTO data_users (nama, nik, jabatan, idcode)
				VALUES ('$nama', '$nik', '$jabatan', '$rfid_code')";
		$queryInput = mysqli_query($conn, $qryInput);
	
		if($qryInput) {
		  $qryTruncate = "TRUNCATE `rfid_code`";
		  $queryTruncate = mysqli_query($conn, $qryTruncate);
	
		  header('Location: ?simpan=ok');
		}
	}	
?>
<br>
<main class="container">
	<h1 class="slideInDown animated" style="margin-bottom: 25px !important;">New User</h1>
	<div class="form-style-5 slideInDown animated">
		<form action="?action=save" method="POST" enctype="multipart/form-data">
			<fieldset>
				<input type="text" name="rfid_code" id="rfid_code" placeholder="RFID CODE" readonly>
				<input class="input" type="text" name="nama" id="nama" placeholder="NAMA LENGKAP" required>
				<input class="input" type="text" name="jabatan" id="jabatan" placeholder="JABATAN" required>
				<input class="input" type="text" name="nik" id="nik" placeholder="NIK" required>			  	
			</fieldset>
			<div class="simple-keyboard"></div>
			<br>
			<button type="submit" name="user_pro_upd" class="user_pro_upd">Save</button>

			<script src="js/simple-keyboard.js"></script>
			<script src="js/my-simple-keyboard.js"></script>
		</form>
	</div>	
</main>
</body>
</html>