Doubt with php redirect from a dialog box

Asked

Viewed 115 times

1

I have the following php file called remove.php, with it I am removing a record from a table, after removed I display a dialog box returning whether it can be removed or not, I would like that by clicking on OK in this dialog box I could direct myself to another php file.

I was afraid to use the header("Location: lista.php"), but when I use the dialogue box on the screen and I’m already redirected to the other file, I also tried to use the header("refresh: 5; url=lista.php"), but I was also unsuccessful.

Someone more experienced could help me?

<?php   
    include("cabecalho.php");
    include("conecta.php");

    function removeProduto($conexao, $id){
        $query = "delete from colaboradores where cpf = '{$id}'";
        return mysqli_query($conexao, $query);
    }

    $id = $_POST['id'];
    removeProduto($conexao, $id);
    $ret = mysqli_affected_rows($conexao);

    if($ret == 1){
?>

<script>
    confirm('colaborador excluído com sucesso!');
</script>

<?php   
    }else if($ret == -1){
?>

<script>
    confirm('colaborador não pode ser excluído!');
</script>

<?php
    }
?>

1 answer

0


This should be done by JS not PHP

You can do:

if($ret == 1){
?>
        <script>
        confirm('colaborador excluído com sucesso!');
        window.location = 'https://example.com/lista.php';
        </script>
<?php       
}else if($ret == -1){
?>    
        <script>
        confirm('colaborador não pode ser excluído!');
        window.location = 'https://example.com/lista.php';
        </script>
<?php    
}
?>

Or simpler:

?>    
        <script>
        confirm('colaborador <?php echo $ret == 1 ? "excluído com sucesso!" : "não pode ser excluído!" ?>');
        window.location = 'https://example.com/lista.php';
        </script>
<?php
  • thanks for the help William. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.