ListarProductos(); function ListarProductos() { fetch("consulta.php", { method: 'POST' }).then(response => response.text()).then(response => { resultado.innerHTML = response; }).catch(error => console.log(error)); } registrar.addEventListener("click", (e) => { e.preventDefault(); fetch("ajax.php", { method: 'POST', body: new FormData(form) }).then(response => response.text()).then(response => { if (response == "ok") { Swal.fire({ icon: 'success', title: 'Registrado', showConfirmButton: false, timer: 1500 }); idp.value = ""; form.reset(); ListarProductos(); } else if (response == "modificado") { Swal.fire({ icon: 'success', title: 'Modificado', showConfirmButton: false, timer: 1500 }); idp.value = ""; form.reset(); ListarProductos(); registrar.value = "Registrar"; } else { Swal.fire({ icon: 'error', title: 'Error al registrar', showConfirmButton: false, timer: 1500 }); } }).catch(error => console.log(error)); }); function eliminar(id) { Swal.fire({ title: 'Esta seguro de eliminar?', icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Si!', cancelButtonText: 'No' }).then((result) => { if (result.isConfirmed) { fetch("eliminar.php", { method: "POST", body: JSON.stringify({ id: id }) }).then(response => response.text()).then(response => { ListarProductos(); Swal.fire({ icon: 'success', title: 'Eliminado', showConfirmButton: false, timer: 1500 }) }).catch(error => console.log(error)); } }); } function editar(id) { fetch("editar.php", { method: "POST", body: JSON.stringify({ id: id }) }).then(response => response.json()).then(response => { idp.value = response.id; codigo.value = response.codigo; producto.value = response.producto; precio.value = response.precio; cantidad.value = response.cantidad; registrar.value = "Actualizar"; }).catch(error => console.log(error)); }