Send an array of JavaScript to PHP

0

I'm starting in this of JavaScript and PHP, I have a problem that is already desperate, the goal is to send an array of JavaScript to a file for it to process. I have the following code in the body of my HTML

<body>

    <table id="TblDestinos" border="1">
        <thead>
            <tr bgcolor="FFFDC1">
                <th>Nombre</th>
                <th>Direccion</th>
                <th>CP</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Destino 1</td>
                <td>Destino Dirección 1</td>
                <td>CP. Destino 1</td>
            </tr>
            <tr>
                <td>Destino 2</td>
                <td>Destino Dirección 2</td>
                <td>CP. Destino 2</td>
            </tr>
        </tbody>
    </table>



        <script>

            var Tabla = document.getElementById("TblDestinos");   
            var arrayTabla = [];
            var arrayFila = [];

            for (var i = 1; i < Tabla.rows.length; i++)
            {

                arrayFila = [];
                CeldasDeFila = Tabla.rows[i].getElementsByTagName('td');

                var Dest_Nomb = CeldasDeFila[0].innerHTML.toLowerCase();
                var Dest_Dir = CeldasDeFila[1].innerHTML.toLowerCase();
                var Dest_CP = CeldasDeFila[2].innerHTML.toLowerCase();

                arrayFila.push(Dest_Nomb);
                arrayFila.push(Dest_Dir);
                arrayFila.push(Dest_CP);

                arrayTabla.push(arrayFila);

            }


function toObject(array) {
  var rv = {};
  for (var i = 0; i < array.length; ++i)
    rv[i] = array[i];
  return rv;
}

var obj = toObject(arrayTabla);
var MiJSON = JSON.stringify(obj);

 //Como envío MiJSON a PHP? y como recupero el array en PHP?

    </script>


</body>

I hope you can help me, I stayed in the part where I convert the array to an object and from there I do not know how to send it to a PHP file, and how to receive it in PHP and convert it to array again.

Thank you very much in advance. I already researched but I do not understand the examples very well.

P.D. Use Windows 7, PHP 7.

A thousand thanks.

    
asked by Carlos Daniel Zárate Ramírez 21.06.2018 в 23:28
source

1 answer

0

You must send your data as follows:

var Tabla = document.getElementById("TblDestinos");   

var destinos = new Array();
var direcciones = new Array();
var codigospostales = new Array();

for (var i = 1; i < Tabla.rows.length; i++)
{

  CeldasDeFila = Tabla.rows[i].getElementsByTagName('td');

  var Dest_Nomb = CeldasDeFila[0].innerHTML.toLowerCase();
  var Dest_Dir = CeldasDeFila[1].innerHTML.toLowerCase();
  var Dest_CP = CeldasDeFila[2].innerHTML.toLowerCase();

  destinos.push(Dest_Nomb);
  direcciones.push(Dest_Dir);
  codigospostales.push(Dest_CP);

}

var datos = new Object();

datos.destinos = destinos;
datos.direcciones = direcciones;
datos.codigospostales = codigospostales;

// Enviar por ajax la variable datos
console.log(datos);

/*

Para recibirlos por php recorres el array enviado

$destinos = $_POST['destinos'];
$direcciones = $_POST['direcciones'];
$codigospostales = $_POST['codigospostales'];

foreach ($direcciones as $key => $value) {
  $direccion = $value;
  $destino = $destinos[$key];
  $codigopostal = $codigospostales[$key];

  echo $direccion.", ".$destino.", ".$codigopostal;
}

*/
<table id="TblDestinos" border="1">
  <thead>
    <tr bgcolor="FFFDC1">
      <th>Nombre</th>
      <th>Direccion</th>
      <th>CP</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Destino 1</td>
      <td>Destino Dirección 1</td>
      <td>CP. Destino 1</td>
    </tr>
    <tr>
      <td>Destino 2</td>
      <td>Destino Dirección 2</td>
      <td>CP. Destino 2</td>
    </tr>
  </tbody>
</table>

Greetings.

    
answered by 22.06.2018 в 00:13