Index.php
<?php
if (isset($_GET["pesan"])) {
$pesan = "<p>{$_GET["pesan"]} </p>";
} else {
$pesan = "";
}
/* ambil nilai nama dan email jika ada */
if (isset($_GET["nama"])) {
$nilai_nama = $_GET["nama"];
} else {
$nilai_nama = "";
}
if (isset($_GET["email"])) {
$nilai_email = $_GET["email"];
} else {
$nilai_nama = "";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>form</title>
<link rel="stylesheet" href=<?php echo "/aqil_web/style.css"; ?>>
</head>
<body>
<h1>Halaman Form</h1>
<?php
echo $pesan;
?>
<div>
<form action="proses.php" method="POST">
<fieldset>
<legend align="">Latihan Form</legend>
<p>
<label for=""> Nama : </label>
<input type="text" name="nama" value="<?php echo $nilai_nama ?>" id="">
</p>
<p>
<label for=""> Email : </label>
<input type="text" name="email" value="<?php echo $nilai_email ?>" id="">
</p>
<p>
<label for=""> Belajar PHP</label>
<input type="checkbox" name="belajar" value="Master PHP" id="">
</p>
<p>
<input type="submit" name="submit" value="Proses Data" id="">
</p>
</fieldset>
</form>
</div>
</body>
</html>
proses.php
<?php
if (!isset($_POST["submit"])) {
header("Location: index.php");
die();
}
$nama = trim($_POST["nama"]);
$email = trim($_POST["email"]);
/* email untuk dikirim kembali */
$query_nama = "nama=" . urlencode($nama);
$query_email = "email=" . urlencode($email);
$query_isi = "&$query_nama&$query_email";
/* Untuk ngecek apakah sudah diisi atau belum */
if (empty($nama)) {
$pesan = urlencode("Nama harus di isi");
header("Location: index.php?pesan={$pesan}{$query_isi}");
die();
}
if (empty($email)) {
$pesan = urlencode("Email harus di isi");
header("Location: index.php?pesan={$pesan}{$query_isi}");
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>proses</title>
</head>
<body>
<h1>Halaman Proses</h1>
<?php
echo "Nama: $nama <br>";
echo "Email: $email <br>";
if (isset($_POST["belajar"])) {
echo "Belajar: " . $_POST["belajar"] . "<br>";
}
?>
</body>
</html>
Tidak ada komentar:
Posting Komentar