Java Script, PHP - Send ID of a php loop to a JavaScript function

0

Well, I have a delete button that has a href addressed to the php delete file. I would like to display a confirmation message before sending the record to the delete page. I used the confirm () method of java script to bar the href of the link and it worked, but I wanted to display a more custom message and for that, I used a javaScript framework called sweetAlert. Through the onclick function in the link, I call the function of the confirmation message, and in the function, if the option is true, I send through window.location.href to the php delete file. My question: Since there are records coming through a while loop directly from the database, how can I send the record ID for the function responsible for the confirmation message? Since the record identifier code (pk) is in a php variable and the function is js. Here is 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");
  }
});

}

Thank you in advance

    
asked by anonymous 17.11.2016 / 23:45

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
    
18.11.2016 / 01:11
0

You can pass the PHP code on the function call in onclick and mount the link within 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
    // ...
}
                                    
18.11.2016 / 02:09