-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.php
More file actions
102 lines (89 loc) · 2.96 KB
/
Copy pathtask3.php
File metadata and controls
102 lines (89 loc) · 2.96 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
$name = $email = $comment = "";
$agree = false;
$errors = [];
$success = false;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = trim($_POST["name"] ?? '');
$email = trim($_POST["mail"] ?? '');
$comment = trim($_POST["comment"] ?? '');
$agree = isset($_POST["agree"]);
// Валидация "name"
if (strlen($name) < 3 || strlen($name) > 20) {
$errors[] = "Name must be between 3 and 20 characters.";
}
if (preg_match('/\d/', $name)) {
$errors[] = "Name cannot contain digits.";
}
// Валидация "mail"
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format.";
}
// Валидация "comment"
if (empty($comment)) {
$errors[] = "Comment cannot be empty.";
}
// Валидация галочки
if (!$agree) {
$errors[] = "You must agree with data processing.";
}
// Если нет ошибок
if (empty($errors)) {
$success = true;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Write a comment</title>
<style>
body { font-family: monospace; }
.form-container { max-width: 600px; margin: 30px auto; }
label { display: block; margin: 10px 0 5px; }
input, textarea { width: 100%; padding: 5px; }
.error { color: red; }
.success { color: green; }
.checkbox-label { display: flex; align-items: center; gap: 5px; }
</style>
</head>
<body>
<div class="form-container">
<h1>#write-comment</h1>
<?php if (!empty($errors)): ?>
<div class="error">
<ul>
<?php foreach ($errors as $e): ?>
<li><?= htmlspecialchars($e) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success">
<p><b>Thank you!</b> Your comment was submitted successfully.</p>
<p>Name: <?= htmlspecialchars($name) ?></p>
<p>Mail: <?= htmlspecialchars($email) ?></p>
<p>Comment: <?= nl2br(htmlspecialchars($comment)) ?></p>
</div>
<?php endif; ?>
<form method="POST">
<label>Name:
<input type="text" name="name" value="<?= htmlspecialchars($name) ?>">
</label>
<label>Mail:
<input type="email" name="mail" value="<?= htmlspecialchars($email) ?>">
</label>
<label>Comment:
<textarea name="comment" rows="6"><?= htmlspecialchars($comment) ?></textarea>
</label>
<label class="checkbox-label">
<input type="checkbox" name="agree" <?= $agree ? 'checked' : '' ?>>
Do you agree with data processing?
</label>
<input type="submit" value="Send">
</form>
</div>
</body>
</html>