modifacion en el metodo de validarYEnviarFormulario
This commit is contained in:
parent
6e8afe9501
commit
ddad222152
vista/js
|
@ -142,52 +142,50 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async function validarYEnviarFormulario(e) {
|
||||||
// Función para validar y enviar el formulario
|
|
||||||
function validarYEnviarFormulario(e) {
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
if (seleccionados.length === 0) {
|
if (seleccionados.length === 0) {
|
||||||
mostrarMensaje('Por favor, seleccione al menos un asiento.');
|
mostrarMensaje('Por favor, seleccione al menos un asiento.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nombreCliente = document.getElementById('nombre_cliente').value.trim();
|
const nombreCliente = document.getElementById('nombre_cliente').value.trim();
|
||||||
if (nombreCliente === '') {
|
if (nombreCliente === '') {
|
||||||
mostrarMensaje('Por favor, ingrese el nombre del cliente.');
|
mostrarMensaje('Por favor, ingrese el nombre del cliente.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preparar los datos para enviar
|
const formData = new FormData();
|
||||||
const asientosIds = seleccionados.map(asiento => asiento.id);
|
formData.append('nombre_cliente', nombreCliente);
|
||||||
const datos = {
|
seleccionados.forEach(asiento => {
|
||||||
asientos: asientosIds,
|
formData.append('asientos[]', asiento.id);
|
||||||
nombre_cliente: nombreCliente
|
|
||||||
};
|
|
||||||
|
|
||||||
// Enviar los datos mediante fetch
|
|
||||||
fetch('../controlador/venta.php', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(datos)
|
|
||||||
})
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(result => {
|
|
||||||
if (result.success) {
|
|
||||||
// Redireccionar a la página de comprobante
|
|
||||||
window.location.href = result.redirect || 'comprobante.php';
|
|
||||||
} else {
|
|
||||||
mostrarMensaje(result.mensaje || 'Error al procesar la venta', 'error');
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
console.error('Error:', error);
|
|
||||||
mostrarMensaje('Error de conexión con el servidor', 'error');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
try {
|
||||||
|
const response = await fetch('../controlador/procesarVenta.php', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
|
||||||
|
const responseText = await response.text(); // Capturar respuesta como texto para depurar
|
||||||
|
console.log('Respuesta del servidor:', responseText);
|
||||||
|
|
||||||
|
// Intentar parsear como JSON
|
||||||
|
const data = JSON.parse(responseText);
|
||||||
|
|
||||||
|
if (!data.success) {
|
||||||
|
mostrarMensaje(data.message || 'Error en la venta'); // Cambiar 'mensaje' a 'message'
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mostrarMensaje('Venta confirmada con éxito', 'success');
|
||||||
|
setTimeout(() => location.reload(), 2000);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error al enviar la venta:', error);
|
||||||
|
mostrarMensaje('Error de conexión con el servidor', 'error');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Función para mostrar mensajes
|
// Función para mostrar mensajes
|
||||||
|
|
Loading…
Reference in New Issue