Skip to content

Instantly share code, notes, and snippets.

@indrakaw
Last active April 13, 2016 14:47
Show Gist options
  • Save indrakaw/add9ebb11df2793b0f724c67b7de4cf4 to your computer and use it in GitHub Desktop.
Save indrakaw/add9ebb11df2793b0f724c67b7de4cf4 to your computer and use it in GitHub Desktop.
PHP Form Validation

A very simple PHP Validation.

<?php
// Default gender value
$gen = ($_GET['gender'] == "" || empty($_GET['gender'])) ? "m" : $_GET['gender'] ;
?>
<!DOCTYPE html>
<html>
<head>
<title>FORM VALIDASI :: Input</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1 id="title">FORM VALIDASI :: Input</h1>
<form class="common" action="form_output.php" method="post">
<?php if ($_GET['error']): ?><strong class="error">DATA ERROR! Form yang anda masukan tidak benar!</strong><?php endif ?>
<div>
<label>Nama Lengkap</label>
<input type="text" name="nama" value="<?php echo $_GET['nama'] ?>">
<?php if ($_GET['error'] && empty($_GET['nama'])): ?><strong class="error">*) Harus diisi!</strong><?php endif ?>
</div>
<div>
<label>Jenis Kelamin</label>
<span><input type="radio" name="gender" value="m"<?php echo ($gen == "m") ? " checked" : "" ; ?>>Laki-laki</span>
<span><input type="radio" name="gender" value="f"<?php echo ($gen == "f") ? " checked" : "" ; ?>>Perempuan</span>
<span><input type="radio" name="gender" value="o"<?php echo ($gen == "o") ? " checked" : "" ; ?>>Lainnya</span>
</div>
<div>
<label>Umur (Tahun)</label>
<select name="umur">
<?php
for ($i=10; $i < 60; $i++) {
$current = ($_GET['umur'] == $i) ? " selected" : "" ;
echo "<option value=\"$i\"" . $current . ">". $i ."</option>\n";
}
?>
</select>
</div>
<div>
<label>Alamat E-mail</label>
<input type="email" name="email" value="<?php echo $_GET['email'] ?>">
<?php if ($_GET['error'] && empty($_GET['email'])): ?><strong class="error">*) Harus diisi!</strong><?php endif ?>
</div>
<div>
<label>Masukan masukan (optimal)</label>
<textarea name="pesan"><?php echo $_GET['pesan'] ?></textarea>
</div>
<div>
<label>Persetujuan</label>
<span>
<input type="checkbox" name="setuju" value="checked" <?php echo $_GET['setuju'] ?>>
Dengan demikian saya pertanggung jawab atas data yang saya input.
</span>
<?php if ($_GET['error'] && empty($_GET['setuju'])): ?><strong class="error">*) Harus dicentang!</strong><?php endif ?>
<div class="btn-row">
<input type="submit" value="submit">
<input type="reset">
</div>
</div>
</form>
</body>
</html>
<?php
if (!empty($_POST['nama']) && !empty($_POST['email']) && $_POST['setuju'] == "checked") {
// echo "BERISI";
} else {
$error = "";
foreach ($_POST as $key => $value) {
$error .= "&" . $key . "=" . $value;
}
// echo $error;
header("Location: form_input.php?error=yes" . $error);
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>FORM VALIDASI :: Output</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1 id="title">FORM VALIDASI :: Output</h1>
<ul class="data">
<?php foreach ($_POST as $key => $value): ?>
<li><strong><?php echo $key ?></strong> <?php echo $value ?></li>
<?php endforeach ?>
<li><a href="form_input.php"><--- Kembali</a></li>
</ul>
</body>
</html>
body {
background: #eee;
max-width: 620px;
margin: auto;
padding: 4px;
}
h1#title {
text-align: center;
text-shadow: 2px 2px #f00;
}
form.common, .data {
background: #fff;
padding: 2%;
list-style: none;
}
form.common > div {
padding: 2px 0;
}
form.common label, .data strong {
width: 140px;
display: inline-block;
word-wrap: break-word;
font-weight: bold;
}
form.common input[type="text"], form.common textarea, form.common input[type="email"] {
width: 200px;
}
form.common select {
min-width: 100px;
}
form.common .btn-row {
margin-left: 140px;
}
form.common .error {
color: red;
display: inline-block;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment