Ajax through the label to href

0

I want to delete a row of Mysql through a link, this way I get very good <a href='eliminar.php/?dato_eliminar= but the page is reloaded. Is there any way to do it with AJAX and the% tag <a> or the label <button> ?

Here is the code that generates the delete buttons for each MYSQL record:

    $.getJSON('cargar_materiales.php', {param1: 'value1'}, function(json, textStatus) { 
$.each(json, function(index, val) {
$(".tabla_materiales").append("<tr>"+"<td>"+val.id+"</td>"+"<td>"+val.nombre+"</td>"+"<td>"+val.codigo+"</td>" +"<td>"+val.precio+"</td>" +"<td>"+val.descripcion+"</td>" +"<td>"+  "<a href='eliminar.php/?dato_eliminar=" + val.codigo +"'>Eliminar</a>" +"</td>" + "</tr>")
});
});

});

    
asked by Javier 17.12.2018 в 21:01
source

1 answer

0

We will add a class to <a> and add javascript.void(0) to remove the default event from the <a> tag. We will also create an attribute called code-val to store the code:

<a href='javascript:void(0)' code-val="+val.codigo+" class='btneliminar'>Eliminar</a>

Then when we click on the class of the button we will take the value of the attribute code-val and send it to the url. When executed execute the part of success , which you have not specified

$(".btneliminar").click(function(){
  var dato=$(this).attr("code-val");
  $.ajax({
     url:"eliminar.php",
     data:{dato_eliminar:dato},
     success:function(result){
         //Aquí poner el codigo luego de ejecutarse
     }
  });
});
    
answered by 17.12.2018 / 21:27
source