-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bobby_tables(PHP Form).txt
78 lines (76 loc) · 2.18 KB
/
Bobby_tables(PHP Form).txt
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
<?php
$name = "";
$character = "";
$email = "";
$birth_year = 1969;
$validation_error = "";
$existing_users = ["admin", "guest"];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim(htmlspecialchars($_POST["name"]));
$raw_name = htmlspecialchars($_POST["name"]);
if(in_array($raw_name, $existing_users)) {
$validation_error .= "This name is taken. <br>";
} else {
$name = $raw_name;
}
$character = $_POST["character"];
$raw_character = $_POST["character"];
if(in_array($raw_character, ["wizard", "mage", "orc"])) {
$character = $raw_character;
} else {
$validation .= "You must pick a wizard, mage, or orc. <br>";
}
$email = $_POST["email"];
$raw_email = $_POST["email"];
if(filter_var($raw_email, FILTER_VALIDATE_EMAIL)) {
$email = $raw_email;
} else {
$validation_error .= "Invalid email. <br>";
}
$birth_year = $_POST["birth_year"];
$raw_birth_year = $_POST["birth_year"];
$options = ["options" => ["min_range" => 1900, "max_range" => date("Y")]];
if(filter_var($raw_birth_year, FILTER_VALIDATE_INT, $options)) {
$birth_year = $raw_birth_year;
} else {
$validation_error .= "That can't be your birth year. <br>";
}
}
?>
<h1>Create your profile</h1>
<form method="post" action="">
<p>
Select a name: <input type="text" name="name" value="<?php echo $name;?>" >
</p>
<p>
Select a character:
<input type="radio" name="character" value="wizard" <?php echo ($character=='wizard')?'checked':'' ?>> Wizard
<input type="radio" name="character" value="mage" <?php echo ($character=='mage')?'checked':'' ?>> Mage
<input type="radio" name="character" value="orc" <?php echo ($character=='orc')?'checked':'' ?>> Orc
</p>
<p>
Enter an email:
<input type="text" name="email" value="<?php echo $email;?>" >
</p>
<p>
Enter your birth year:
<input type="text" name="birth_year" value="<?php echo $birth_year;?>">
</p>
<p>
<span style="color:blue;"><?= $validation_error;?></span>
</p>
<input type="submit" value="Submit">
</form>
<h2>Preview:</h2>
<p>
Name: <?=$name;?>
</p>
<p>
Character Type: <?=$character;?>
</p>
<p>
Email: <?=$email;?>
</p>
<p>
Age: <?=date("Y")-$birth_year;?>
</p>