-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.php
More file actions
67 lines (57 loc) · 1.48 KB
/
create.php
File metadata and controls
67 lines (57 loc) · 1.48 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
<?php
require 'config.php';
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
if ($name === '' || $email === '') {
$errors[] = "Nombre y email son obligatorios.";
}
if (empty($errors)) {
$sql = "INSERT INTO contacts (name, email, phone) VALUES (:name, :email, :phone)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':name' => $name,
':email' => $email,
':phone' => $phone
]);
header('Location: index.php');
exit;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Crear contacto</title>
</head>
<body>
<h1>Crear contacto</h1>
<?php if (!empty($errors)): ?>
<?php foreach ($errors as $error): ?>
<p style="color:red;"><?= htmlspecialchars($error) ?></p>
<?php endforeach; ?>
<?php endif; ?>
<form method="post">
<label>
Nombre<br>
<input type="text" name="name" required>
</label>
<br><br>
<label>
Email<br>
<input type="email" name="email" required>
</label>
<br><br>
<label>
Teléfono<br>
<input type="text" name="phone">
</label>
<br><br>
<button type="submit">Guardar</button>
<a href="index.php">Cancelar</a>
</form>
</body>
</html>