call php function from javascript

0

good afternoon friends I have a problem that breaks my head and I can not solve this code I have

function llenar_tabla_json(){
    $('.cargando').hide();
    $(".table-carrito tbody").empty();
    $("#carrito_ventas .table tbody").html("");
    $.post("comprobantes/listado_carrito",{accion:''},function(data){
        var tabla = ""; var i=1; var total = 0;
        if(jQuery.isEmptyObject(data) == false){
            $.each(data, function(index, val) {
                tabla += '<tr>'
                        +'<td class="text-center">'+i+'</td>'
                        +'<td class="text-center">'
                        +'<input type="number" class="cantidad" min="1" value="'+val['cantidad']+'" data-item="'+val['id']+'|'+val['precio']+'|'+val['unidad']+'|'+val['nombre']+'|'+val['codigo_servicio']+'" style="width:50px;height:25px;text-align: center;padding: 0;">'
                        +'</td>'
                        +'<td class="text-center">'+val['codigo_servicio']+'</td>' 
                        +'<td class="text-center">'+val['unidad']+'</td>'
                        +'<td class="text-center">'+val['nombre']+'</td>'
                        +'<td class="text-center">'
                        +'<input type="number" step="any" class="precios" value="'+val['precio']+'" data-item="'+val['id']+'|'+val['cantidad']+'|'+val['unidad']+'|'+val['nombre']+'|'+val['codigo_servicio']+'" style="width:60px;height:25px;text-align: center;padding: 0;">'
                        +'</td>'                    
                        +'<td class="text-center">'+(val['precio'] * val['cantidad']).toFixed(2)+'</td>'
                        +'<td class="text-center">'
                        +'<a href="comprobantes/unset_producto"><i class="fa fa-trash-o"></i></a>'+
                        +'</td>'                  
                        +'</tr>';
                i++;
                total += (val['precio'] * val['cantidad']);
            });
            $("#carrito_ventas .table > tbody > tr").remove();
            $(".table-carrito tbody").append(tabla);
        }else{
            tabla = '<tr>'
                        +'<td class="text-center text-danger" colspan="6">'
                            +'Agregue items para empezar'
                        +'</td>'
                    +'</tr>';
            $(".table-carrito tbody").append(tabla); 
        }'

I want to send the item number to remove from the shopping cart to this function

The address is comprobantes/unset_producto and I have to add the variable i

public function unset_producto($unique_id)
    {
        unset($_SESSION["carrito_v"][$unique_id]);

    }
    
asked by Fernando Abel Gonzales Ch 06.07.2018 в 00:52
source

1 answer

1

to see that you are already using jquery, then you could do it in the following way:

in this line:

+'<a href="comprobantes/unset_producto"><i class="fa fa-trash-o"></i></a>'+

add the data-id_item attribute and the "btn_delete_item" class, so it looks like this:

+'<a href="comprobantes/unset_producto" class="btn_delete_item" data-id_item="'+val['id']+'"><i class="fa fa-trash-o"></i></a>'+

and this part outside your "fill_table_json" function

$(".table-carrito").on("click", ".btn_delete_item", function(e){
    e.preventDefault();
    var btn = $(this);
    $.ajax({
        url: btn.attr('href'),
        type: "GET",  //GET si el backend recibe los datos por URL o POST si los recibe con el método literal "POST"
        dataType: "json",
        data: {unique_id: btn.data('id_item')}
    })
    .done(function(data){
        alert(data.status);
    });
});

and the PHP code would look like this:

<?php

public function unset_producto($unique_id){
    //al ver que la recibes como argumento entonces diría que mandas 
    //la clave por la url, aquí depende mucho de como este construido tu backend 
    //para ver como recibes los datos y ver como las vas a mandar con AJAX

    unset($_SESSION["carrito_v"][$unique_id]);
    echo json_encode(["status" => "El producto con clave " . $unique_id . " ha sido borrado"]);
}

I hope and this little help is useful to you :)

    
answered by 06.07.2018 / 02:02
source