i’m writing a code in PHP, where a response of a if condition call a Javascript function with SweetAlert2.
Basicaly when a register is updated the button call a PHP function and this one call a Javscript function if it runs with success. Thsi javascript function is a SweetAlert2 which show a message and should wait a click on “Ok” button to redirect the user to another URL.
The problem is the message appears really fast and don’t wait to click, just close the message itself. The redirect is not working as well.
I already try this javascript function out of the PHP code and it is running fine.
How can I fix it please?
PHP code
function UpdateReg() {
$Titulo = $_REQUEST['f_titulo'];
$Descricao = $_REQUEST['f_desc'];
$Valor = $_REQUEST['f_valor'];
$cod = $_GET['cod'];
if ($GLOBALS['$conexao']->connect_error) {
die("Falha de Conexão: " . $GLOBALS['$conn']->connect_error);
}
$sql = "UPDATE produtos set titulo = '$Titulo', descricao = '$Descricao', valor = '$Valor', WHERE cod_produto = $cod";
if (mysqli_query($GLOBALS['$conexao'], $sql)) {
echo "<script>ChangeOk('produto')";
} else {
echo "Erro: " . $sql . "<br>" . mysqli_error($GLOBALS['$conexao']);
}
mysqli_close($GLOBALS['$conexao']);
}
}
Javascript
function ChangeOk(tipo)
{
Swal.fire({
title: "Feito!",
text: "Registro alterado com sucesso",
icon: 'success',
showConfirmButton: true
}).then((result) => {
if (result.isConfirmed) {
window.location = "home.php?tipo=" + tipo
}
})
}