how to extract data from a JavaScript array and insert them into a PHP query one by one in different inserts

0

Good day programmers I have a doubt I would like you to help me with something I have the stored value of quantity that I store it within cantid = []; and then those data with a json I take them to a query in PHP the values that are not fixes are not problems because I take them out and insert them but the values that are in arrangement would have a problem when inserting them and I would like them to help me insert them with a for

function usu(){
    var persona=document.getElementById("persona").innerHTML;
    var lista=document.getElementById("lista");
    var pedido=document.getElementById("lista-pedido");
    var radioid;
    var cantid=[];
    var produc=[];

    if(lista.childNodes.length>1){
        var tamaño=lista.childNodes.length;
        for(var i=1; i<tamaño; i++){
            var mesa=lista.childNodes[i];
            var radio=mesa.childNodes[1];
            if(radio.checked){
                console.log(radio.value);
                radioid=radio.value;
            }
        }
    }
    if(pedido.childNodes.length>1){
    var tamaño2=pedido.childNodes.length;
    for(var i=1; i<tamaño2; i++){
        var mesa2=pedido.childNodes[i];
        cantid.push(document.getElementById(mesa2.id+"-1").innerHTML);
    }   
    }    

    if(pedido.childNodes.length>1){
        var tamaño3=pedido.childNodes.length;
        for(var i=1; i<tamaño3; i++){
            var mesa3=pedido.childNodes[i];
            produc.push(mesa3.value);  
            produc.console(mesa3.value); 
        }   
    }     

    var prueba=511;
    var usu=document.getElementById("usu").innerHTML;
    $.getJSON("./json/ingresar.php", {prueba:prueba, usu:usu, radioid:radioid}, 
    function(resulta){
        var valor = JSON.stringify(resulta);
        console.log("lo que se supone que tiene valor " + valor);
    });
}

How you enter them into the database :

<?php
include '../Conexion_DB.php';
$c = new Conexion_DB();
$conn = $c->__constructor();
$id = $_GET["prueba"];
$usu = $_GET["usu"];
$radioid = $_GET["radioid"];

if($conn){
    $sql = "insert into Ventas_Cuentas (Cuenta, Usuario, Mesa) 
values(".$id.",'".$usu."','".$radioid."')";
    $stmt = sqlsrv_query($conn, $sql);
    echo json_encode('jaló', JSON_UNESCAPED_UNICODE);
} else {
    echo json_encode('no me conecté :(');
}
?>

The radioid and usu data and test have no problem but the ones that I need help are those of quantity and production.

thanks

    
asked by costadark1212 02.08.2018 в 18:34
source

1 answer

0

Yes, good. It's really not a problem Although better send them by post for the size of the data.

$.ajax({
        let arr=JSON.stringify(cantid)//aquí guardas como STRINGIFY el contenido del arreglo
       url: './json/ingresar.php',  
       dataType: 'JSON', 
			data:{prueba:prueba, usu:usu, radioid:radioid,'cantid':arr},
                cache: false,                    
                type: 'POST',
                success: function(resulta)
				{
                var valor = JSON.stringify(resulta);
        console.log("lo que se supone que tiene valor " + valor);
					   
                }
	});

Then the PHP works the variable "$ _POST ['cantid']" and considers it an array.

I hope to have explained. Greetings

    
answered by 02.08.2018 / 19:51
source