Receiving data from a Table on a php page

Asked

Viewed 25 times

1

Greetings!! I’m trying to do something I don’t know if it’s possible. I have a table in an html page, which I took and turned into an array of objects that apart from Ajax send the same array to another php page that later I will visualize it in table form.


    //Envia para o php
    function mandar_tabela() {



    var indices = [];

    //Pega os indices
    $('#tabela-preços thead tr th').each(function() {
        indices.push($(this).text());
    });

    var arrayItens = [];

    //Pecorre todos os produtos
    $('#tabela-preços tbody tr').each(function(index) {

        var obj = {};

        //Controle o objeto
        $(this).find('td').each(function(index) {
            obj[indices[index]] = $(this).text();
        });

        //Adiciona no arrray de objetos
        arrayItens.push(obj);

    });

    //Mostra dados pegos no console
    console.log(arrayItens);

    //Envia para o php
    $.ajax({
        type: "POST",
        url: "imprimirfatura1.php",
        data: arrayItens,
        success: function() {
            alert('Deu tudo certo');
        },
    });
}

However, when it comes to receiving in php page I can’t even see the array of sent objects. When I open the page printtable.php I find absolutely nothing. I tried to use the POST method but could not, six the code piece of the page printtable.php next:

<?php 
 $array[] = $_POST['arrayItem'];
$pagina="<!DOCTYPE html>
<html lang='en'>
[...]

How do I receive this object array with ajax, and view on the page printtable.php

  • if you make a $.ajax will not leave the current page, should make a Submit to the other tab, and put the array for example in a field input type Hidden so you can request on the other page

  • Ricardo Pontual, I didn’t notice. I just updated the question, putting all the javascript function

  • a request ajax returns to the same page, so I figured want to send the data to another page and from the other page create the table is this?

  • That’s right Ricardo, I want to send the data to another page.

  • because then the way to do this is to put everything in a form tag, for example <form action='imprimirtabela.php'>...aqui vão todos os campos, </form> and put the result of the array in a field within the tag form. If you have questions let us know

  • So with that, I would have to put my table inside a form, then convert it into an array, right?

  • I put in an answer to make it clearer

Show 2 more comments

2 answers

0

To explain further, I will put an example:

HTML:

<form id="formImprimir" action='imprimirtabela.php'>

   ... aqui tudo que precisa ser enviado

   ... e esse input que vai conter o array de objetos
   <input type="hidden" id="array-itens" name="array-itens" />
</form>

Javascript:

... aqui monta o objeto arrayItens
// Serializa o objeto
var arrayItensSerializado = JSON.stringify(arrayItens);
// Seta o campo input com o valor
document.getElementById("array-itens").value = arrayItensSerializado;
// Faz submit do form
document.getElementById("formImprimir").submit();
// Não precisa do ajax.

PHP (print table.php):

// Pega o valor de "array-itens"
$json = $_POST['array-itens'];
// Converte para objeto
$itens = json_decode($json);

0

I didn’t quite understand the question but, in case you want to insert some data into an AJAX table using jQuery, it would be more or less like this:

$(document).ready(function() {
  EnviaDataTable();
})

function EnviaDataTable() {
  $.ajax({
    url: "imprimirfatura1.php",
    cache: false,
    type: "POST",
    data: arrayItens,
    success: function(dados) {
      var tabela = $("#table").DataTable();
      tabela.clear().draw();

      $.each(dados, function(index, value) {
        tabela.row.add([
          value.column1,
          value.column2,
          value.column3,
          value.column4
          //...
        ]).draw();
      })

      tabela.columns.adjust().draw();

    },
    error: function(error) {
      alert(error);
    }
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="table" name="table" width="100%">
  <thead>
  </thead>
  <tbody></tbody>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.