Pass javascript parameter to a php page by a button

Asked

Viewed 400 times

1

I need to delete a record in the database by id, I am displaying my table with the records through ajax and jquery, however I need a button that excludes by the id of the record, I have created the page deleta.php (works) however I can not pass the id to php, follows the ajax that carries 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;
                })
  • Forehead window.location.href='excluir.php?id=' + this.id; for the this is the button clicked on this function.

  • did not work, I have to pass the id tbm in $('#table')??

2 answers

3

Your code is incomplete, I don’t see where your function is confirm_delete and the selector $('apagar') is wrong.

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

Also changes the onload for onclick, I think that’s what you want, when it’s clicked.

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>');

      }
    }
  });
});

0

In part:

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

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

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

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

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

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

  • jsfiddle.net/yr2aezw4/1 - I have htm and ajax

Browser other questions tagged

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