Skip to content

vote.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<html>
  <head>
    <style type="text/css"> 
      body { font-size: 12px;font-family: Verdana; }
      .errors h3, 
      .errors li { color: #FF0000;}
      .errors li { margin: 5px 0px;}
    </style>
  </head>
  <body>
<?php
  date_default_timezone_set('America/Chicago');
  $error_messages = array();
  $month = "";
  $day = "";
  $year = "";
  if (isset($_POST["submit"]))
  {
    //We need to capture empty values and display friendly error messages
    if (empty($_POST["month"]))
    {
      array_push($error_messages, 'Please Input a value for Month');
    }
    else
    {
      $month = $_POST["month"];
    }
    if (empty($_POST["day"]))
    {
      array_push($error_messages, 'Please Input a value for Day');
    }
    else
    {
      $day = $_POST["day"];
    }
    if (empty($_POST["year"]))
    {
      array_push($error_messages, 'Please Input a value for Year');
    }
    else
    {
      $year = $_POST["year"];
    }
  }
?>

  <?php if (isset($error_messages) && count($error_messages) > 0) : ?>
    <h3>There were errors that prevented from verifying your age able to vote.</h3>
    <ul class="errors">
      <?php foreach($error_messages as $error) : ?>
        <li><?= $error; ?></li> 
      <?php endforeach; ?>
    </ul>
  <?php endif; ?>

  <h3>Enter your brithday:</h3>
    <form action="vote.php" method="POST">
      <input type="text" size=2 name="month" value="<?= $month ?>">-
      <input type="text" size=2 name="day" value="<?= $day ?>">-
      <input type="text" size=4 name="year" value="<?= $year ?>">
      <input type=submit value="submit" name="submit">
      <input type=reset value="clear">
    <form>

<?php
  if (isset($_POST["submit"]) && count($error_messages) < 1)
  {     
    $birthday = strtotime("$month-$day-$year");  
    $birthday_plus_18_years = strtotime('+18 years', $birthday);
    $today =  time();
    $over_18 = ($birthday_plus_18_years < $today ? true : false);
  }
?>

      <?php if (isset($over_18) && $over_18 == true) :?>
        <h4>Congratulations you are old enough to vote in the United States</h4>
      <?php elseif (isset($over_18) && $over_18 == false) : ?>
        <h4>You are not old enough to vote in the United States</h4>
      <?php else : ?>
        <h4>Tell me how old you are</h4>  
      <?php endif; ?>
  </body>
</html>