Ajax-Error using Json in PHP

0

I'm using Json to make an insert in MySQL using ajax, but I do not get any results and the insertion is not done, what am I doing wrong?

JS code

$("#btnprueba").click(function () {
            var array1 = [];
            $("#tabla .DataRow").each(function () {

                var firstTableData = {};
                var Aidi = $(this).find('td').eq(0).text().trim();
                    firstTableData.ID_Articulo = $(this).find('td').eq(0).text().trim();
                    firstTableData.Descripcion = $(this).find('td').eq(1).text().trim();
                    firstTableData.Valor_Venta = $(this).find('td').eq(2).text().trim();
                    firstTableData.Cantidad = $(this).find('td').eq(3).text().trim();
                    firstTableData.Subtotal = $(this).find('td').eq(4).text().trim();
                    array1.push(firstTableData);

            });

            var JsonValues = JSON.stringify(array1);
                alert(JsonValues);
            $.ajax({
                type: "POST",
                url: "GuardarDatosFactura2.php",
                data: "J="+JsonValues,
                success: function(response){
                    alert(response);
                },
                error: function(e){
                    console.log(e.message);
                },

            });

        });

Json Obtained in the variable JsonValues

[{"ID_Articulo":"001","Descripcion":"Caramelos","Valor_Venta":"6500","Cantidad":"2","Subtotal":"13000"}]

PHP code

?php 
header("Content-Type: application/json; charset=UTF-8");

$CON = mysqli_connect("localhost","root","","BDfactura") or die ("error");

$data = json_decode($_POST['J'],false);

    $ID=$data->ID_Articulo;
    $Canti=$data->Cantidad;   
    $Vlr=$data->Valor_Venta;

    $cadena2 = "INSERT INTO ItemXVenta (IdArticulo, Cantidad, ValorVenta) VALUES ('$ID','$Canti','$Vlr')";
    $create2 = mysqli_query($CON,$cadena2);

    if($cadena2){
        $MSG= "Se guardaron los datos";
    }
    else{
        $MSG= "No se guardaron los datos";
    }
    echo($MSG);

? >

    
asked by Gammath.rar 30.04.2018 в 04:21
source

1 answer

1

What happens when you click on your #btnprueba element? Is this element of your DOM a button type submit ? Or is it a <a></a> tag?

Try this code

$("#btnprueba").click(function (e) {
            e.preventDefault();
            var array1 = [];
            $("#tabla .DataRow").each(function () {

                var firstTableData = {};
                var Aidi = $(this).find('td').eq(0).text().trim();
                    firstTableData.ID_Articulo = $(this).find('td').eq(0).text().trim();
                    firstTableData.Descripcion = $(this).find('td').eq(1).text().trim();
                    firstTableData.Valor_Venta = $(this).find('td').eq(2).text().trim();
                    firstTableData.Cantidad = $(this).find('td').eq(3).text().trim();
                    firstTableData.Subtotal = $(this).find('td').eq(4).text().trim();
                    array1.push(firstTableData);

            });

            var JsonValues = JSON.stringify(array1);
                alert(JsonValues);
            $.ajax({
                type: "POST",
                url: "GuardarDatosFactura2.php",
                data: "J="+JsonValues,
                success: function(response){
                    alert(response);
                },
                error: function(e){
                    console.log(e.message);
                },

            });

        });

With the line of e.preventDefault(); we will avoid the natural behavior of input's type submit , now the alert or console.log that you have within your ajax

should already be displayed     
answered by 30.04.2018 / 17:30
source