简体   繁体   中英

Javascript not executing PHP file when a button is clicked

I have a button that uses a function to make some calculations and then insert data to a table. It was working ok, but now it doesn't work and I have no idea why. I hope someone can help me out. Everything seems ok to me and I don't know what else to do.

this is the button code:

<button type="button" name="btnAgregarProducto" class="btn btn-xs btn-primary" id="command-add" onclick="rowFunction('dgvDetalleFactura')" data-row-id="0">
                            <span class="glyphicon glyphicon-plus"></span> Producto</button>

this is the JS code:

<script>
    /*Insertar productos en el detalle*/
     function rowFunction(Tabla) {

        /*Ir a factura, calcular todo y luego traerlo y meterlo en la tabla*/

        $.ajax({
                        url: 'Logica/Factura.php',
                        type: 'post',
                        data: 
                        {
                           btnAgregarProducto:'AgregarProducto', 
                           PV:document.getElementById('PrecioVenta').value,
                           IV:document.getElementById('IV').value,
                           Des:document.getElementById('Descuento').value,
                           Cant:document.getElementById('Cantidad').value,
                           IDp:document.getElementById("IDProducto").value,
                           NombreP:document.getElementById("NombreProducto").value;
                           UM:document.getElementById("UnidadMedida").value;

                        },
                        dataType: 'json',
                        success:function(response){

                            var len = response.length;

                            if(len > 0){
                                var IdProducto = response[0]['IDProducto'];
                                var Nombrep = response[0]['NombreProducto'];
                                var PV = response[0]['PrecioVenta'];
                                var iv = response[0]['Impuesto'];
                                var Desc = response[0]['Descuento'];
                                var UM = response[0]['UM'];

                                var a = document.getElementById(Tabla).insertRow(1);

                                var Codigo = a.insertCell(0);
                                var Producto = a.insertCell(1);
                                var Cantidad = a.insertCell(2);
                                var Precio = a.insertCell(3);
                                var Medida = a.insertCell(4);
                                var Impuesto = a.insertCell(5);
                                var Descuento = a.insertCell(6);
                                var Subtotal = a.insertCell(7);
                                var Total = a.insertCell(8);
                                var BotonBorrar = a.insertCell(9);

                                Codigo.innerHTML =IdProducto;   
                                Producto.innerHTML =Nombrep;
                                Cantidad.innerHTML =document.getElementById("Cantidad").value;
                                Precio.innerHTML =PV;
                                Medida.innerHTML =document.getElementById("UnidadMedida").value;
                                Impuesto.innerHTML =document.getElementById("IV").value;
                                Descuento.innerHTML =document.getElementById("Descuento").value;
                                Subtotal.innerHTML = 'Subtotal';
                                Total.innerHTML = "Total";

                                BotonBorrar.innerHTML='<button class="btn btn-primary btn-xs my-xs-btn" type="button" onClick="borrarFila(this)" >'
+ '<span class="glyphicon glyphicon-remove"></span>Borrar</button>';
                            }

                        }
                    });

                    return false;
     }
  </script>

PHP File code:

  <?php 
session_start();

require ("../Conexion/Conexion.php");

//Verificar que el usuario haya pulsado el boton de agregar producto
if(isset($_POST['btnAgregarProducto']))
{
    $IDProducto=$_POST['IDp'];
    $NombreProducto=$_POST['NombreP'];
    $PrecioVentaSinIV=$_POST['PV'];
    $ImpuestoVenta=$_POST['IV'];
    $Descuento=$_POST['Des'];
    $Cantidad=$_POST['Cant'];
    $UnidadMedida=$_POST['UM'];

    if($ImpuestoVenta==0) /*Sino Tiene Impuesto*/
    {
        $MontoExento=$PrecioVentaSinIV*$Cantidad;  /*Precio sin impuesto x Cantidad sino tiene impuesto*/
        $MontoGravado=0; 

    }
    else /*Si Tiene Impuesto*/
    {
        $MontoExento=0;
        $MontoGravado=$PrecioVentaSinIV*$Cantidad;  /*Precio sin impuesto x Cantidad si tiene impuesto*/
    }

    if($UnidadMedida=='sp' AND $ImpuestoVenta==0)
    {
        $MontoSExento=$PrecioVentaSinIV*$Cantidad; /*Precio sin impuesto x Cantidad*/
        $MontoSGravado=0;
    }
    else
    {
        $MontoSExento=0/*Precio con impuesto x Cantidad*/
        $MontoSGravado=$PrecioVentaSinIV*$Cantidad;
    }       

    $MontoIV=($PrecioVentaSinIV*$Cantidad)*$Descuento)*$ImpuestoVenta; /*((Precio sin impuesto x Cantidad)*Descuento) x IV*/
    $MontoOtroI=0;/*se va a cambiar mas adelante*/
    $MontoDescuento=($PrecioVentaSinIV*$Cantidad)*$Descuento);/*(Precio sin impuesto x Cantidad)*Descuento*/


    $SubtotalL=($PrecioVentaSinIV*$Cantidad);/*Precio sin impuesto x Cantidad*/
    $TotalL=$SubtotalL-$MontoDescuento+$MontoIV;/*Subtotal-Descuento+Impuesto*/

    $users_arr[] = array("IDProducto" => $IDProducto, "NombreProducto" => $NombreProducto,"PrecioVentaConIV" => $, "Impuesto" =>$ImpuestoVenta, "Descuento" =>$Descuento, "UM"=>$UnidadMedida,"Cantidad"=>$Cantidad);
    }

    // encoding array to json format
    echo json_encode($users_arr);
    exit;
}

?>

NombreP:document.getElementById("NombreProducto").value; UM:document.getElementById("UnidadMedida").value;

There should be , not ; at end of these lines.

Did you change(added or removed) anything in the src js? eg <script src="assets/respond/respond.js"></script>

The most common problem for me when something "stoped Workind" in Javascript is those kind of changes. If you did so undo it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM