SOURCE CODE:-
<!DOCTYPE html>
<html>
<style>
.error{
color:red;
}
body {
font-family:arial;
}
</style>
<body>
<form method="post" action="<?php echo ($_SERVER["PHP_SELF"]);?>"> <!-- ($_SERVER["PHP_SELF"]) will return the filename of the currently executing script.-->
<?php
$name = $email = $gender = $address =$dob = "";
$nameError=$genderError=$emailError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameError = "Name is required";//Error if name is empty
} else {
$name = ($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameError = "Only letters and white space allowed"; //Error if name contains characters other than letters and white space.
}
}
$dob = ($_POST["dob"]);
if (empty($_POST["email"])) {
$emailError = "E-mail is required"; //Error is e-mail is not entered
} else {
$email = ($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$emailError="Invalid e-mail format!"; //Error if the e-mail format is wrong
}
}
$address = ($_POST["address"]);
if (empty($_POST["gender"])){
$genderError="Gender is required"; //Error if the gender is not entered
}
else{
$gender = ($_POST["gender"]);
}
}
?>
<h2> Data Form </h2>
Name: <input type="text" name="name" size="70">
<span class="error">*<?php echo $nameError;?></span>
<br><br>
Date of Birth: <input type="date" name="dob" size="20">
<br><br>
Gender:
<input type="radio" name="gender" value="female" >Female
<input type="radio" name="gender" value="male">Male
<span class="error">*<?php echo $genderError;?></span>
<br><br>
Address: <textarea name="address" rows="5" cols="40"> </textarea>
<br><br>
E-mail: <input type="text" name="email" size="25">
<span class="error">*<?php echo $emailError;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $dob;
echo "<br>";
echo $gender;
echo "<br>";
echo $address;
echo "<br>";
echo $email;
?>
</body>
</html>
OUTPUT:-
(Error shown when name is entered as dinu@123,and when e-mail is entered as dinu)
Comments
Post a Comment