
<?php
$arrLevel	= array("Siswa","Admin");


# KETIKA DIKLIK
if(isset($_POST['btnSave']))
	{
		# PERIKSA VARIABLE
		$txtNama		= $_POST['txtNama'];
		$txtUser		= $_POST['txtUser'];
		$txtPassword	= $_POST['txtPassword'];
		$cmbLevel		= $_POST['cmbLevel'];
		
		# SAMPAIKAN PESAN ERROR
		$pesanError = array();
			if(trim($_POST['txtNama'])=="") 
				{
					$pesanError[] = "Data <b>NAMA</b> tidak boleh kosong, harus diisi !";		
				}	
			if(trim($_POST['txtUser'])=="") 
				{
					$pesanError[] = "Data <b>USERID</b> tidak boleh kosong, harus diisi !";		
				}
			if(trim($_POST['txtPassword'])=="") 
				{
					$pesanError[] = "Data <b>PASSWORD</b> tidak boleh kosong, harus diisi !";		
				}
			if(trim($_POST['cmbLevel'])=="") 
				{
					$pesanError[] = "Data <b>LEVEL</b> tidak boleh kosong, harus diisi !";		
				}
		# VALIDASI USERNAME, JIKA SUDAH ADA DITOLAK
			$cekSql	= "SELECT userid FROM userweb WHERE userid='$txtUser'";
			$cekQry	= mysql_query($cekSql, $koneksidb) or die ("Eror Query".mysql_error()); 
			if(mysql_num_rows($cekQry)>=1)
				{
					$pesanError[] = "Maaf, User ID <b> $txtUser </b> sudah ada, ganti dengan yang lain";
				}	
				
		# JIKA ADA PESAN ERROR MAKA TAMPILKAN ERROR
		if (count($pesanError)>=1 )
			{
				echo "<div class='mssgBox'>";
				echo "<img src='images/attention.png'> <br><hr>";
				$noPesan=0;
				foreach ($pesanError as $indeks=>$pesan_tampil) 
				{ 
					$noPesan++;
					echo "&nbsp;&nbsp; $noPesan. $pesan_tampil<br>";	
				} 
				echo "</div> <br>"; 
			}
		else
			{
				
				# MASUKKAN DATA KE SQL			
				$mySql	= "INSERT INTO userweb ( nama, userid, password, level) 
						   VALUES ( '$txtNama', '$txtUser', '".md5($txtPassword)."', '$cmbLevel')";
							
				$myQry	= mysql_query($mySql, $koneksidb) or die ("Gagal query".mysql_error());
				if($myQry)
					{
						echo "<meta http-equiv='refresh' content='0; url=?page=Data-Userweb&Act=Sucsses'>";
					}
				exit;
			}
	}		

?> 


<table >
	<form    method="post" enctype="multipart/form-data" > 	
		<tr>
			<th colspan="3" ><h2>TAMBAH DATA SISWA</h2> </th>
		</tr>
		<tr height="30">
			<td width="150"><strong>Nama </strong></td>
			<td><b>:</b></td>
			<td><input name="txtNama" type="text  size="40" /></td>
		</tr>
		<tr height="30">
			<td><strong>Userid </strong></td>
			<td><b>:</b></td>
			<td><input name="txtUser" type="text"  size="40"/></td></tr>
		<tr height="30">
			<td><strong>Password</strong></td>
			<td><b>:</b></td>
			<td><input name="txtPassword" type="password"  size="40" /></td>
		</tr>
		<tr height="30">
			<td><strong>Level</strong></td>
			<td><b>:</b></td>
			<td><select name="cmbLevel" >
					<option value="BLANK"> </option>
					<?php
						foreach ($arrLevel as $index => $value) 
						{
							if ($value==$dataLevel) 
								{
									$cek="selected";
								} else { $cek = ""; }
							echo "<option value='$value' $cek>$value</option>";
						}
					?>
				</select>
			</td>
		  
			<tr><td>&nbsp;</td>
			<td>&nbsp;</td>
			<td><input type="submit" name="btnSave" value=" SIMPAN " ></td>
		</tr>
	</form>
</table>
