Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $message = "";
- $month_error = "";
- $day_error = "";
- $year_error = "";
- // Create variables for range of options
- $month_options = ["options" => ["min_range" => 1, "max_range" => 12]];
- $day_options = ["options" => ["min_range" => 1, "max_range" => 31]];
- $year_options = ["options" => ["min_range" => 1903, "max_range" => 2021]];
- // create function to validate user input based on each input option range
- function validateInput($type, &$error, $options_arr){
- if (!filter_var($_POST[$type], FILTER_VALIDATE_INT, $options_arr)){
- $error = "* Invalid ${type}";
- return FALSE;
- } else {
- return TRUE;
- }
- }
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- $test_month = validateInput("month", $month_error, $month_options);
- $test_day = validateInput("day", $day_error, $day_options);
- $test_year = validateInput("year", $year_error, $year_options);
- if ($test_month && $test_day && $test_year){
- $message = "Your birthday is: {$_POST["month"]}/{$_POST["day"]}/{$_POST["year"]}";
- }
- }
- ?>
- <form method="post" action="">
- Enter your birthday:
- <br>
- Month: <input type="number" name="month" value="<?= $_POST["month"];?>">
- <span class="error"><?= $month_error;?> </span>
- <br>
- Day: <input type="number" name="day" value="<?= $_POST["day"];?>">
- <span class="error"><?= $day_error;?> </span>
- <br>
- Year: <input type="number" name="year" value="<?= $_POST["year"];?>">
- <span class="error"><?= $year_error;?> </span>
- <br>
- <input type="submit" value="Submit">
- </form>
- <p><?= $message;?></p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement