Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
validation/index.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
135 lines (110 sloc)
3.49 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Validation</title> | |
<style> | |
.error { | |
color: #FF0000; | |
} | |
</style> | |
</head> | |
<body> | |
<?php | |
$nameErr = $addrErr = $emailErr = $howManyErr = $favFruitErr = ""; | |
$name = $address = $email = $howMany = ""; | |
$favFruit = array(); | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
if (empty($_POST["name"])) { | |
$nameErr = "Missing"; | |
} | |
else { | |
$name = $_POST["name"]; | |
} | |
if (empty($_POST["address"])) { | |
$addrErr = "Missing"; | |
} | |
else { | |
$address = $_POST["address"]; | |
} | |
if (empty($_POST["email"])) { | |
$emailErr = "Missing"; | |
} | |
elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) { | |
$emailErr = "Invalid email format."; | |
$email = $_POST["email"]; | |
} | |
else { | |
$email = $_POST["email"]; | |
} | |
if (!isset($_POST["howMany"])) { | |
$howManyErr = "You must select 1 option"; | |
} | |
else { | |
$howMany = $_POST["howMany"]; | |
} | |
if (empty($_POST["favFruit"])) { | |
$favFruitErr = "You must select 1 or more"; | |
} | |
else { | |
$favFruit = $_POST["favFruit"]; | |
} | |
$brochure = $_POST["brochure"]; | |
if (!$nameErr && !$addrErr && !$emailErr && !$howManyErr && !$favFruitErr) { | |
echo "Success!<br>"; | |
$options = array( $name, $address, $email, $howMany, $favFruit, $brouchure); | |
foreach ( $options as $option ) { | |
if (is_array($option)) { | |
echo join(", ", $option) . "<br>"; | |
} else { | |
echo "$option" . "<br>"; | |
} | |
} | |
} | |
} | |
?> | |
<form method="POST" | |
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> | |
Name <input type="text" name="name" value="<?php echo htmlspecialchars($name);?>"> | |
<span class="error"><?php echo $nameErr;?></span> | |
<br> | |
Address <input type="text" name="address" value="<?php echo htmlspecialchars($address);?>"> | |
<span class="error"><?php echo $addrErr;?></span> | |
<br> | |
Email <input type="text" name="email" value="<?php echo htmlspecialchars($email);?>"> | |
<span class="error"><?php echo $emailErr;?></span> | |
<br> | |
<input type="radio" name="howMany" | |
<?php if (isset($howMany) && $howMany == "zero") echo "checked"; ?> | |
value="zero"> 0 | |
<input type="radio" name="howMany" | |
<?php if (isset($howMany) && $howMany == "one") echo "checked"; ?> | |
value="one"> 1 | |
<input type="radio" name="howMany" | |
<?php if (isset($howMany) && $howMany == "two") echo "checked"; ?> | |
value="two"> 2 | |
<input type="radio" name="howMany" | |
<?php if (isset($howMany) && $howMany == "twoplus") echo "checked"; ?> | |
value="twoplus"> More than 2 | |
<span class="error"><?php echo $howManyErr; ?></span> | |
<br> | |
<select name="favFruit[]" size="4" multiple=""> | |
<?php | |
$options = array("apple", "banana", "plum", "pomegranate", "strawberry", "watermelon"); | |
foreach ($options as $option) { | |
echo '<option value="' . $option . '"'; | |
if (in_array($option, $favFruit)) { | |
echo " selected"; | |
} | |
echo ">" . ucfirst($option) . "</option>"; | |
} | |
?> | |
</select> | |
<span class="error"><?php echo $favFruitErr; ?></span> | |
<br> | |
Would you like a brochure? <input type="checkbox" name="brochure" value="Yes" <?php if ($brochure) { echo "checked"; } ?>> | |
<br> | |
<input type="submit" name="submit" value="Submit"> | |
</form> | |
</body> | |
</html> |