PHP Form Require, Include, Include Once

Source Code : index.php

<h2>Trisna Almuti Form PHP Require,
    Include, Include Once</h2>
<?php
    require ("require.php");
?>

<?php include_once 'footer.php';?>

Source Code : require.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP 7</title>
</head>
<style>
    body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 18px;
    }
    hr {
        border: 5px solid #515151;
        border-radius: 5px;
    }
    .isi {
      border: 3px solid black;
      border-radius: 5px;
      padding: 5px;
    }
</style>
<style>
.error {color: #FF0000;}
</style>
<body>  

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Nama anda harus diisi";
  } else {
    $name = test_input($_POST["name"]);
  }
  
  if (empty($_POST["email"])) {
    $emailErr = "Email anda harus diisi";
  } else {
    $email = test_input($_POST["email"]);
  }
    
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "Gender harus diisi";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<div style="align-items: center; display: block;">
<p><span class="error">* kolom harus diisi</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name:<br> <input type="text" name="name">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  E-mail:<br> <input type="text" name="email">
  <span class="error">* <?php echo $emailErr;?></span>
  <br><br>
  Website:<br> <input type="text" name="website">
  <span class="error"><?php echo $websiteErr;?></span>
  <br><br>
  Comment:<br> <textarea name="comment" rows="5" cols="40"></textarea>
  <br><br>
  Jenis Kelamin:
  <input type="radio" name="gender" value="Wanita">Perempuan
  <input type="radio" name="gender" value="Pria">Pria
  <input type="radio" name="gender" value="Tidak di ketahui">Tidak di ketahui
  <span class="error">* <?php echo $genderErr;?></span>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>
<div class="isi">
  <h2>Hasil Input:</h2>
<?php
echo "Nama : $name";
echo "<br>";
echo "Email : $email";
echo "<br>";
echo "Website : $website";
echo "<br>";
echo "Komentar : $comment";
echo "<br>";
echo "Jenis Kelamin : $gender";
?>
</div>
</div>
</body>
</html>

Source Code : footer.php
<?php
echo "<hr>";
echo "<p>Web Ini dibuat oleh Trisna Almuti</p>";
?>

Hasil :

Posting Komentar

Lebih baru Lebih lama