-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.php
More file actions
100 lines (88 loc) · 3.46 KB
/
Copy pathadd.php
File metadata and controls
100 lines (88 loc) · 3.46 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
<?php
// Include database connection and helper functions
require_once 'config/db.php';
require_once 'includes/functions.php';
// Initialize variables
$errors = [];
$contact = [
'first_name' => '',
'last_name' => '',
'phone' => '',
'email' => ''
];
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Sanitize input data
$contact = [
'first_name' => trim($_POST['first_name']),
'last_name' => trim($_POST['last_name']),
'phone' => trim($_POST['phone']),
'email' => trim($_POST['email'])
];
// Validate contact data
$errors = validateContactData($contact);
// If no errors, add contact to database
if (empty($errors)) {
if (addContact($contact)) {
// Redirect to index page on success
header('Location: index.php');
exit;
} else {
$errors['db'] = "Failed to add the contact. Please try again.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add New Contact - PHP Contacts App</title>
<link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<div class="container">
<h1>Add New Contact</h1>
<a href="index.php" class="back-link">Back to Contacts</a>
<?php if (isset($errors['db'])): ?>
<div class="alert alert-error">
<?php echo $errors['db']; ?>
</div>
<?php endif; ?>
<form action="add.php" method="POST" class="contact-form">
<div class="form-group">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" value="<?php echo htmlspecialchars($contact['first_name']); ?>" required>
<?php if (isset($errors['first_name'])): ?>
<div class="error"><?php echo $errors['first_name']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" value="<?php echo htmlspecialchars($contact['last_name']); ?>" required>
<?php if (isset($errors['last_name'])): ?>
<div class="error"><?php echo $errors['last_name']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<label for="phone">Phone Number:</label>
<input type="tel" id="phone" name="phone" value="<?php echo htmlspecialchars($contact['phone']); ?>" required>
<?php if (isset($errors['phone'])): ?>
<div class="error"><?php echo $errors['phone']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($contact['email']); ?>" required>
<?php if (isset($errors['email'])): ?>
<div class="error"><?php echo $errors['email']; ?></div>
<?php endif; ?>
</div>
<div class="form-group">
<button type="submit" class="btn">Add Contact</button>
</div>
</form>
</div>
</body>
</html>