send array from ajax to php

1

hello to all I have the following form in which I add products and when clicking on add they are stored in the table as in the image, As you can see in the image I have 3 products and when clicking on preview what it does is store all the records of the table in an array

this is the code

  

JQuery (ajax)

$('#btnbtn').on('click',function(){
                /*(nose si es la mejor manera de almacenar los datos de mi tabla en un array, 
                si alguien sabe una mejor manera ,le estaria muy agradecido) ↓*/
               var datosArray=new Array();
                $('#tablaProductosIngresados > #tablaPI').each(function () {
                var codigoArray = $(this).find('td').eq(0).html();
                var descripcionArray = $(this).find('td').eq(1).html();
                var unidadMedidaArray = $(this).find('td').eq(2).html();
                var PrecioUniArray= $(this).find('td').eq(3).html();
                var cantidadArray = $(this).find('td').eq(4).html();
                var descuentoArray = $(this).find('td').eq(5).html();
                var totalArray = $(this).find('td').eq(6).html();
                var itemArray = $(this).find('td').eq(8).html();
                var tipoExistenciaArray = $(this).find('td').eq(9).html();
                 var fechaVigenciaArray = $(this).find('td').eq(10).html();

                 valor=new Array(codigoArray, descripcionArray, unidadMedidaArray,PrecioUniArray,cantidadArray,descuentoArray,totalArray,itemArray,tipoExistenciaArray,fechaVigenciaArray);

                  datosArray.push(valor);


                 var jsonString = JSON.stringify(valor);

                   // console.log(jsonString);
                     $.ajax({
                          url:'insertVistaPrevia.php',
                          type:'POST',
                          data:{btnbtn:jsonString},    
                          cache: false,
                          success:function(data){

                           console.log(data);                         
                          } 

                     });
                      return false;
                });
              })

and so I receive them in my php code

  

php

<?php 
$data = json_decode($_POST['btnbtn']);

foreach ($data as $datos) {

    echo $datos;


}?>

my problem is that it only shows me the first record of my table

I do not know how to put my code of the table since columns are generated as you click on a button to add .....

I've searched and everyone says it's done with "" "JSON.stringify" ""

but it does not come out, I hope you help me

    
asked by Jhona JM 30.11.2018 в 22:35
source

2 answers

0

This would be the correct synthesis:

data: {
    "codigoArray": codigoArray, 
    "descripcionArray": descripcionArray,
    "unidadMedidaArray": unidadMedidaArray,
    "cantidadArray": cantidadArray,
    "nombre": "Iván",
    "codigo": "1234"
}

PHP Code:

$dataCodigo = $_POST['codigoArray'];
$dataDescripcion = $_POST['descripcionArray'];
$dataUnidad = $_POST['unidadMedidaArray'];
$dataCantidad = $_POST['cantidadArray'];

echo $dataCodigo;
echo $dataDescripcion;
echo $dataUnidad;
echo $dataCantidad;

This is because the AJAX technology in data allows you to use a JSON object, an array or a query string.

    
answered by 01.12.2018 в 10:47
0

I managed to do what I needed (to pass an array of js to php) I managed to convert it to json to pass it to php in the following way, if it happened to someone.

  

js

              function articulo (codigo, descripcion,unidadMedida,PrecioUnitario,cantidad,descuento,total,item,tipoExistencia,fechaVigencia )
                {
                    this.codigo = codigo;
                    this.descripcion = descripcion;
                    this.unidadMedida = unidadMedida;
                    this.PrecioUnitario = PrecioUnitario;
                    this.cantidad = cantidad;
                    this.descuento = descuento;
                    this.total = total;
                    this.item = item;
                    this.tipoExistencia = tipoExistencia;
                    this.fechaVigencia = fechaVigencia;
                }

              $('#btnbtn').on('click',function(){                   
               var datosArray=new Array();
                $('#tablaProductosIngresados > #tablaPI').each(function () {
                 valor=new Array($(this).find('td').eq(0).html(),
                                 $(this).find('td').eq(1).html(),
                                 $(this).find('td').eq(2).html(), 
                                 $(this).find('td').eq(3).html(), 
                                 $(this).find('td').eq(4).html(), 
                                 $(this).find('td').eq(5).html(), 
                                 $(this).find('td').eq(6).html(), 
                                 $(this).find('td').eq(8).html(), 
                                 $(this).find('td').eq(9).html(),
                                 $(this).find('td').eq(10).html());
                 datosArray.push(valor);
                });

                        var i, n = datosArray.length;
                        var tmp = new Array();
                        for (i=0; i<n; i++)
                        {
                            tmp[i] = new articulo( datosArray[i][0], datosArray[i][1], datosArray[i][2], datosArray[i][3], datosArray[i][4], datosArray[i][5], datosArray[i][6], datosArray[i][7], datosArray[i][8], datosArray[i][9] );
                        }
                        var mObj = new Object;
                        mObj.datosArray = tmp;
                        var objJSON = JSON.stringify(mObj);
                        //console.log(objJSON);

                        $.ajax({
                          url:'insertVistaPrevia.php',
                          type:'POST',
                          data:{data:objJSON},    
                          cache: false,
                          success:function(data){

                             console.log(data); 

                          } 

                     });
                      return false;



              })
    
answered by 05.12.2018 в 16:01