pause page and resume after user confirmation

Asked

Viewed 246 times

0

Guys I have the following structure in php:

            <?php
            // Verifica se cancela o cadastro
            if ((!empty($action)) and ($action == "cancelar")) {

                ?>
                <script>
                    confirm ('Tem certeza que quer cancelar?>');
                </script>
                <?php
                die;

                // Aqui executa as operaçøes no BD
            }
        ?>

Good need to make php process user information only if the user confirms Alert.

You can do this using jQuery?

  • You have to do it with ajax, or create a cookie (with true/false value) with javascript and then redirect/Reload the page. You can see the value of the cookie in php: $_COOKIE['NOME DO COOKIE']

  • You’d better put this confirm() on the cancellation button onClick.

2 answers

3


You can do this right on the cancel link:

<a title="Cancelar" onclick="return confirm('Você tem certeza?');" href="seuscript.php?ation=cancelar&id=XX">Cancelar</a>

But if you really want to do with php follow an example:

<?php

    $action = $_GET['action'];
    // Verifica se cancela o cadastro
    if ((!empty($action)) and ($action == "cancelar")) {  ?>
        <a title="Cancelar" href="seuscript.php?action=cancelar-confirmado&id=XX">VocÊ tem certeza que deseja cancelar?</a>
    <?php
    }elseif ((!empty($action)) and ($action == "cancelar-confirmado")){
        # seu codigo de cancelamento aqui
        echo "Cancelado com sucesso!!";
    }

?>

1

Just because JS is "inside" PHP does not mean confirm() will prevent the execution of PHP, even because, in general, when displaying the confirm() PHP has already run.

Another detail is that the confirm() how the documentation shows returns true or false so you need to check the answer with a if.

Without more details complicates giving a better example but the correct would be something like this:

document.addEventListener("DOMContentLoaded", function(event) {
  var btCancela = document.getElementById('cancelar');
  btCancela.addEventListener('click', function(e){
    if( confirm('Deseja realmente cancelar?') ){
      //aqui é o pulo do gato, você precisar fazer uma requisição ajax ou redirecionar para uma outra URL, ou seja la como você apaga o registro do banco.
      console.log('usuario confirmou o cancelamento');
    }
  });
});
<button id="cancelar">CANCELAR</a>

Javascript and PHP are different languages for different purposes, unfortunately they do not communicate the way you imagined.

Browser other questions tagged

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