Java Script, PHP - Sends ID of a php loop to a Javascript function

Asked

Viewed 165 times

0

Guys, I have a delete button that has a href directed to the php delete file. I would like to display a confirmation message before sending the registration to the exclusion page. I used the confirm() method of the java script to bar href from the link and it worked, but I wanted to display a more customized message and to do so, I used a Java framework called sweetAlert. Through the onclick function in the link, I call the confirmation message function, and in the function, if the option is true, I send via window.location.href to the delete php file. My question: Since these are records coming through a while loop directly from the database, how can I send the registry ID for the function responsible for the confirmation message? Since the registry identifier code (pk) is in a php variable and the function is js. Follows the code:

//código do link

<a onclick="confirma_excluir_cliente('');" href="exclui_cliente.php?codigo=<?=$linha['codigo']?>" class="btn btn-danger"><i class="fa fa-trash"></i></a>

//funcao da mensagem
var confirma_excluir_cliente = function(){

  swal({
    title: "Are you sure?",
    text: "You will not be able to recover this imaginary file!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, delete it!",
    closeOnConfirm: false,
  },
  function(isConfirm){
  if (isConfirm) {
    window.location.href="exclui_cliente.php"
  } else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
  }
});

}

thanks in advance

2 answers

0


Just pass the code as a function parameter:

//código do link (removi as partes desnecessárias pra resposta)

<a href="#" onclick="confirma_excluir_cliente(<?=$linha['codigo']?>);"></a>

//funcao da mensagem
var confirma_excluir_cliente = function(codigo){
    console.log(codigo);
    // continua a função
  • Ricardo, thank you so much for answering, it worked! ;)

0

You can pass the PHP code in the function call in onclick and mount the link inside the function:

<a onclick="confirma_excluir_cliente(<?=$linha['codigo']?>);" href="#" class="btn btn-danger"><i class="fa fa-trash"></i></a>

//funcao da mensagem
var confirma_excluir_cliente = function(codigo){
    // ...
    window.location.href="exclui_cliente.php?codigo=" + codigo
    // ...
}
  • Waister, it worked out, thank you so much for the tip!

Browser other questions tagged

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