pause page and resume after user confirmation

0

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
            }
        ?>

Well I need to make php process the user information only if the user confirms the alert.

Can you do this using jQuery?

    
asked by anonymous 01.09.2016 / 14:10

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!!";
    }

?>
    
01.09.2016 / 14:47
1
Just because JS is "inside" PHP does not mean that confirm() will prevent the execution of PHP, so in general, when displaying confirm() , PHP has already finished executing.

/ p>

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

Without more details complicates to give a better example and the correct one 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.

    
01.09.2016 / 14:46