Pass a javascript parameter to a php page for a button

1

I need to delete a record in the database by the id, I am displaying my table with the records through ajax and jquery, but I need a button that excludes the id of the record, I already created the page deleta.php (it works) I can not pass the id to php, it follows the ajax that loads the tebela:

$(document).ready(function(){


    $('#tabela').empty(); //Limpando a tabela
    $.ajax({
        type:'post',        //Definimos o método HTTP usado
        dataType: 'json',   //Definimos o tipo de retorno
        url: 'getDados.php',//Definindo o arquivo onde serão buscados os dados
        success: function(dados){
            for(var i=0;dados.length>i;i++){
                //Adicionando registros retornados na tabela

                $('#tabela').append('<tr><td>'+dados[i].nome+'</td><td>'+dados[i].endereco+'</td><td>'+dados[i].telefone+'</td><td><button id="apagar" onload="confirm_delete('+dados[i].id+')">Excluir</td></tr>');

            }
        }
    });
});
$('apagar').click(function(){
                    window.location.href='excluir.php?id='+dados[i].id;
                })
    
asked by anonymous 17.05.2017 / 17:36

2 answers

3

Your code is incomplete, I do not see where you have the confirm_delete function and the $('apagar') selector is wrong.

Even though it was $('#apagar') would be wrong because of event delegation and because the IDs have to be unique. I suggest you pass id and this to confirm_delete and use ajax to delete and remove the line in success .

It also changes onload to onclick , I think that's what you want, when clicked.

It would look like this:

function confirm_delete(btn, id) {
    var apagar = confirm('Quer mesmo apagar?');
    if (apagar) {
      $.ajax({
        type: 'post',
        url: 'excluir.php',
        data: {id: id},
        success: function() {
          $(btn).closest('tr').remove();
        }
      });
    }
}
$(document).ready(function() {

  $('#tabela').empty(); //Limpando a tabela
  $.ajax({
    type: 'post', //Definimos o método HTTP usado
    dataType: 'json', //Definimos o tipo de retorno
    url: 'getDados.php', //Definindo o arquivo onde serão buscados os dados
    success: function(dados) {
      var $tabela = $('#tabela');
      for (var i = 0; dados.length > i; i++) {
        $tabela.append('<tr><td>' + dados[i].nome + '</td><td>' + dados[i].endereco + '</td><td>' + dados[i].telefone + '</td><td><button onclick="confirm_delete(this, ' + dados[i].id + ')">Excluir</td></tr>');

      }
    }
  });
});
    
17.05.2017 / 17:48
0

In part:

$('apagar').click(function(){ window.location.href='excluir.php?id='+dados[i].id; })

You will not be able to access the data variable information as it is only available within the scope of the success function passed as a parameter in ajax.

Your selector for the delete button is probably wrong if you are trying to select by id (#) or class (.).

A simple solution to your code would be to put a cod attribute with the value of the id on the delete button, and so when you click you select the value of the attribute.

$('.apagar').click(function(){ window.location.href='excluir.php?id='+$(this).attr('cod'); })

So that I can give you a more precise answer you could make the rest of the code available.

    
17.05.2017 / 19:16