Confirmation of registration deletion with javascript

Asked

Viewed 538 times

0

Good morning, although there are some questions on the subject, I did not find the appropriate answer so put here my question: I have an DELETE button with the following code:

 <a href='javascript:void(0)' class="btn btn-danger link_exclusao" rel="
 <?=$condominio->id?>">Excluir</a>

which leads to the action_condominio.php file with the following code:

 // Verifica se foi solicitada a exclusão dos dados



 if ($acao == 'excluir'):
    // Exclui o registro do banco de dados
    $sql = 'DELETE FROM condominio WHERE id = :id';
    $stm = $conexao->prepare($sql);
    $stm->bindValue(':id', $id);
    $retorno = $stm->execute();

    if ($retorno):
        echo "<div class='alert alert-success' role='alert'>Registro excluído com sucesso, aguarde você está sendo redirecionado ...</div> ";
    else:
        echo "<div class='alert alert-danger' role='alert'>Erro ao excluir registro!</div> ";
    endif;

    echo "<meta http-equiv=refresh content='3;URL=consulta_condominio.php'>";
endif;
?>

that uses javascript:

/* Atribui ao evento click do link de exclusão na página de consulta a função confirmaExclusao */
var linkExclusao = document.querySelectorAll(".link_exclusao");
if (linkExclusao != null) {
  for (var i = 0; i < linkExclusao.length; i++) {
    (function(i) {
      var id = linkExclusao[i].getAttribute('rel');

      if (linkExclusao[i].addEventListener) {
        linkExclusao[i].addEventListener("click", function() {
          confirmaExclusao(id);
        });
      } else if (linkExclusao[i].attachEvent) {
        linkExclusao[i].attachEvent("onclick", function() {
          confirmaExclusao(id);
        });
      }
    })(i);
  }
}


/* Função para exibir um alert confirmando a exclusão do registro*/
function confirmaExclusao(id){
    retorno = confirm("Deseja excluir esse Registro?")

    if (retorno){

        //Cria um formulário
        var formulario = document.createElement("form");
        formulario.action = "action_condominio.php";
        formulario.method = "post";

        // Cria os inputs e adiciona ao formulário
        var inputAcao = document.createElement("input");
        inputAcao.type = "hidden";
        inputAcao.value = "excluir";
        inputAcao.name = "acao";
        formulario.appendChild(inputAcao); 

        var inputId = document.createElement("input");
        inputId.type = "hidden";
        inputId.value = id;
        inputId.name = "id";
        formulario.appendChild(inputId);

        //Adiciona o formulário ao corpo do documento
        document.body.appendChild(formulario);

        //Envia o formulário
        formulario.submit();
    }
}

but is not deleting the record.

Where is wrong?

  • 1

    Paul what browsers do you want to support? You could do this with ajax without needing to form.submit(). So you are loading the page again when you do Submit.

  • Sergio, good morning ! I’m using an example I saw but only with a form, and it works perfectly but my system is implementing several forms, maybe that’s the problem. It excludes correctly in the customer form but in others not. I searched all references to the`s ID and there is nothing specific id(client) type, I believe it is not updating the variable. I’ll try other functions to see if it works. Hug

  • Paul I can suggest an answer but I would like to understand more about the problem so I am not guessing what you may or may not have. What browsers you want to support?

  • I’m using Chrome, I believe the vast majority use it itself. I tested it right now on safari and gives the same error.

  • You can check if the ID is being printed in the attribute rel? Is there an error in JS? You can show what Chrome reports about the error in console? How are you getting the ID in the PHP file? You can see if the ID is coming in PHP by means of a var_dump or echo or print?

  • Tony, the ID goes like this: rel="<?= $condominio->id? > and arrives like this: echo "<meta http-equiv=refresh content='3;URL=consulta_condominio.php'>"; the error in err.log was this: PHP Notice: Undefined variable: id in /..... /public_html/safe/action_residencial.php on line 63 there was a syntax error: I fixed and solved the problem by creating a javascript for each form with a different name. The function references a file: action_condominio.php and in the other forms the ID was not found. Hug.

  • @Paulofernandocé Have you solved the problem? Mark the question as solved.

  • Resolved ! I don’t know how to mark it...

Show 3 more comments
No answers

Browser other questions tagged

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