-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.html
174 lines (153 loc) · 5.2 KB
/
form.html
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login / Registro</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
main {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
h2 {
margin-bottom: 20px;
color: #333;
}
form {
display: flex;
flex-direction: column;
}
label {
text-align: left;
margin-bottom: 5px;
font-weight: bold;
}
input {
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
p {
margin-top: 15px;
}
.toggle-button {
background-color: transparent;
border: none;
color: #007bff;
cursor: pointer;
font-size: 14px;
text-decoration: underline;
}
.toggle-button:hover {
color: #0056b3;
}
.message {
color: green;
margin-top: 10px;
}
.error {
color: red;
margin-top: 10px;
}
</style>
</head>
<body>
<main>
<h2 id="formTitle">Login</h2>
<form id="authForm">
<div>
<label for="email">Email:</label>
<input type="email" id="email" required />
</div>
<div>
<label for="password">Password:</label>
<input type="password" id="password" required />
</div>
<button type="submit" id="submitButton">Login</button>
</form>
<p id="message" class="message"></p>
<p>
<span id="toggleText">¿No tienes cuenta?</span>
<button type="button" class="toggle-button" id="toggleModeButton">
Regístrate aquí
</button>
</p>
</main>
<script>
const form = document.getElementById('authForm');
const emailInput = document.getElementById('email');
const passwordInput = document.getElementById('password');
const messageElement = document.getElementById('message');
const formTitle = document.getElementById('formTitle');
const submitButton = document.getElementById('submitButton');
const toggleModeButton = document.getElementById('toggleModeButton');
const toggleText = document.getElementById('toggleText');
let isLogin = true; // Estado para determinar si es login o registro
form.addEventListener('submit', async function(e) {
e.preventDefault();
const email = emailInput.value;
const password = passwordInput.value;
const endpoint = isLogin
? 'http://localhost/si/aerolinea/login.php'
: 'http://localhost/si/aerolinea/register.php';
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password }),
});
const result = await response.json();
if (result.status === 'success') {
messageElement.textContent = isLogin
? 'Login exitoso'
: 'Usuario registrado con éxito';
messageElement.className = 'message';
} else {
messageElement.textContent = result.message || 'Ocurrió un error, intenta de nuevo';
messageElement.className = 'error';
}
} catch (error) {
messageElement.textContent = 'Error al conectar con el servidor';
messageElement.className = 'error';
}
});
// Cambiar entre Login y Registro
toggleModeButton.addEventListener('click', function() {
isLogin = !isLogin;
formTitle.textContent = isLogin ? 'Login' : 'Registro';
submitButton.textContent = isLogin ? 'Login' : 'Registrar';
toggleText.textContent = isLogin ? '¿No tienes cuenta?' : '¿Ya tienes cuenta?';
toggleModeButton.textContent = isLogin ? 'Regístrate aquí' : 'Inicia sesión aquí';
messageElement.textContent = ''; // Limpiar mensaje al cambiar de modo
});
</script>
</body>
</html>