Minggu, 31 Mei 2020

Form 1 File Tervalidasi karakter dan email

<?php
if (isset($_POST["submit"])) {
    /* ambil nilai form */
    $nama = htmlentities(strip_tags(trim($_POST["nama"])));
    $email = htmlentities(strip_tags(trim($_POST["email"])));
    $buku = strtolower(htmlentities(strip_tags(trim($_POST["buku"]))));
    $pesan_eror = "";
    $array_buku = ["html uncover", "css uncover", "php uncover"];

    if (empty($nama)) {
        $pesan_eror .= "Nama belum diisi <br>";
    } elseif (strlen($nama) <= 5) {
        $pesan_eror .= "nama harus 5 digit <br>";
    }
    if (empty($email)) {
        $pesan_eror .= "Email belum diisi";
    } elseif (strpos($email, "@") == false) {
        $pesan_eror .= "Penulisan alamat email salah <br>";
    }
    if (empty($buku)) {
        $pesan_eror .= "Buku belum diisi";
    } elseif (!in_array($buku, $array_buku)) {
        $pesan_eror .= "Buku tidak tersedia";
    }

    if ($pesan_eror == "") {
        echo "<h1>Form Berhasil diproses</h1>";
        echo "Nama : $nama <br>";
        echo "Email : $email <br>";
        echo "Buku : $buku";
        die();
    }
} else {
    $pesan_eror = "";
    $nama = "";
    $buku = "";
    $email = "";
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>form</title>
</head>

<body>

    <datalist id="list_buku">
        <option value="php uncover" label="php uncover">
        <option value="html uncover" label="html uncover">
        <option value="css uncover" label="css uncover">
    </datalist>
    <?php
    echo $pesan_eror;
    ?>
    <form action="index.php" method="POST">
        <fieldset>
            <legend align="">Latihan Form</legend>
            <p>
                <label for=""> Nama : </label>
                <input type="text" name="nama" value="<?php echo $nama ?>" placeholder="nama lengkap">
            </p>
            <p>
                <label for=""> Email : </label>
                <input type="text" name="email" value="<?php echo $email ?>" placeholder="alamat email">
            </p>
            <p>
                <label for=""> Buku : </label>
                <input type="text" name="buku" list="list_buku" value="<?php echo $buku ?>" placeholder="Pilih Buku yang tersedia">
            </p>
            <p>
                <input type="submit" name="submit" value="Proses Data" id="">
            </p>
        </fieldset>
    </form>
</body>

</html>

Tidak ada komentar:

Posting Komentar