<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!--
# action defines where the form contents are sent when the form is submitted
# htmlspecialchars() is used here to convert specific HTML characters to their HTML entity names .e.g. > will be converted to >
# $_SERVER is an array with entries filled in at PHP run time; the PHP_SELF key contains the filename of the PHP script that is being executed
# method determines how the form’s contents is submited
# POST means that the form’s content is sent as part of the HTTP request’s body
# trim() function use to remove white space from a single variable start and end point
# php filter to use validate and sanitize data . Validating data = Determine if the data is in proper form.Sanitizing data = Remove any illegal character from the data.
-->
<?php
$name;
$email;
$amount;
$nameerr="";
$emailerr="";
$amounterr="";
// check submit button is clicked
if ($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$amount= trim($_POST["amount"]);
// if fill input field
if (!empty($name) && !empty($email)) {
// validate String
if (!filter_var($name,FILTER_SANITIZE_STRING)) {
$nameerr = " Your Text Is Not Valid ";
}
// Validate Email
if (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$emailerr = " Please Provide A Valid Email Address ";
}
if (!is_numeric($amount)) {
$amounterr =" Please Give Valid Number ";
}
}
if (empty($name)) {
$nameerr = "Name Is Required";
}
if (empty($email)) {
$emailerr = " Please Fill Email Field ";
}
if (empty($amount)) {
$amounterr = " Please Fill Amount Field ";
}
}
?>
<form action="<?php echo htmlspecialchars ($_SERVER['PHP_SELF']);?>" method="post">
<label for="name"> Name </label> <input type="text" name="name" id="name"> <?php echo $nameerr;?>
<br>
<label for="email"> Email </label> <input type="text" name="email" id="email"> <?php echo $emailerr;?> <br>
<label for="amount"> Amount </label> <input type="text" name="amount" id="amount"> <?php echo $amounterr;?> <br>
<button type="submit" name ="submit">Submit</button>
</form>
</body>
</html>