Confirm jQuery by running 2 times

Asked

Viewed 161 times

1

Hello, I have a code that "activates and disables" users. This:

HTML

 <button type='button' class='btn btn-success btn-circle ativar-gestor'>
    <i class='fa fa-check'></i>
    <input type='hidden' value='{$dados['Id']}' name='id'>
 </button>

 <button type='button' class='btn btn-danger btn-circle desativar-gestor'>
    <i class='fa fa-ban'></i>
    <input type='hidden' value='{$dados['Id']}' name='id'>
 </button>

jQuery and Ajax (confirmaAtiv runs 2 times, shows the confirm window 2 times)

//Ativar
$( ".ativar-gestor" ).click(function() {
    var icone        = $(this);
    var id           = $('input', icone).val();
    var confirmaAtiv = confirm('Deseja realmente ativar este gestor?');
    if(confirmaAtiv){
        $.ajax({
            type    : 'POST',
            url     : 'dados/ativar-gestor.php',
            data    : {id : id},
            dataType : 'json',
            success : function(data){
                if(data.status == 1 || data.status == 2){
                    alert(data.msg);
                    window.location.href="index.php?pg=gestores";
                }else{
                    form.get(0).reset();
                    alert(data.msg);
                }
            }
        });
    }
});

//Desativar
$( ".desativar-gestor" ).click(function() {
    var icone    = $(this);
    var id       = $('input', icone).val();
    var confirmaDesat = confirm('Deseja realmente desativar este gestor?');
    if(confirmaDesat){
        $.ajax({
            type    : 'POST',
            url     : 'dados/desativar-gestor.php',
            data    : {id : id},
            dataType : 'json',
            success : function(data){
                if(data.status == 1 || data.status == 2){
                    alert(data.msg);
                    window.location.href="index.php?pg=gestores";
                }else{
                    form.get(0).reset();
                    alert(data.msg);
                }
            }
        });
    }
});

PHP Active Manager (works correctly)

    $id = mysqli_real_escape_string($conn, $_POST['id']);

    //Checa existencia do ID
    $sqlCheck = mysqli_query($conn, "SELECT Id FROM Gestores WHERE Id = {$id}");


    //Ativa
    if(mysqli_num_rows($sqlCheck) == 1){
        if(mysqli_query($conn, "UPDATE Gestores SET Ativo = 1 WHERE Id = {$id}")){
            $retorno = array("status" => 1, "msg" => "Cadastro ativado com sucesso!");
            echo json_encode($retorno);
            exit;
        }else{
            $retorno = array("status" => 0, "msg" => "Ocorreu um erro ao ativar o cadastro. Tente novamente.");
            echo json_encode($retorno);
            exit;
        }
    }else{
        $retorno = array("status" => 2, "msg" => "Cadastro inexistente. O que está tentando fazer?");
        echo json_encode($retorno);
        exit;
    }

Disable works fine. The enable performs correctly but displays twice the dialog box to confirm. What would be wrong with the code?

  • What is the code of "confirm"?

  • var confirmaAtiv = confirm('Do you really want to enable this manager? '); if(confirmaAtiv){ (...)

  • I’m talking about the code of the function "confirm".

  • I don’t understand your question.

  • You are using the function "confirm", to appear to the user the message, the problem can this in this function, you could edit the question and put the code of the function "confirm".

  • 1

    I did a test here, and the message only appeared once, you have no other element on your screen with the "enable-manager" class, you have tried using id.

  • with id works, but since I have multiple lines with this same button, it wouldn’t be right to use id... no?

  • It is true, put several buttons with the same id can not, apparently your code this right, tries to do the following, var confirmaAtiv = confirm('Really want to activate this manager?' + id);, so you will know the id that are activating the click event, so you will know at least which buttons are activating the event when you click.

  • 1

    @Wictorchaves thanks for the availability! I forgot to return here. In fact there are no errors in the actual code. I couldn’t even find the hahaha conflict, I changed the class name and nothing. The solution was to isolate the code from others that were being called on the page. Now it’s ok.

Show 5 more comments
No answers

Browser other questions tagged

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