Handle array objects from AJAX

Asked

Viewed 23 times

0

Good afternoon guys, I read the whole forum and tried several things, but I can’t extract the data from the array to manipulate.

Well, I’m putting together a system where, after making a sale, it saves the sale on a table with the seller’s name and the total sale.

I was able to extract this data and save.

The other part is to take the sale products and save in another table and associate the sale id above in the products.

My Array comes like this:

 array(3) {
  ["vendafinal"]=>
  array(2) {
    [0]=>
    array(3) {
      ["vendaproduto"]=>
      string(10) "Produto 02"
      ["vendaqtd"]=>
      string(2) "10"
      ["vendavalor"]=>
      string(1) "5"
    }
    [1]=>
    array(3) {
      ["vendaproduto"]=>
      string(10) "Produto 01"
      ["vendaqtd"]=>
      string(1) "5"
      ["vendavalor"]=>
      string(2) "10"
    }
  }
  ["vendedor"]=>
  string(4) "Erik"
  ["totalvenda"]=>
  string(3) "100"
}

The Seller and the sale total I managed to pull with:

$vendedor = $_POST['vendedor'];
$totalvenda = $_POST['totalvenda'];
$data = date("Y-m-d");
$hora = date("H:i:s");

Saved at the bank and he displays perfectly. The other part is to save the products in another table, with the quantity and value of the product and also the id of the previous sale. But ta bone, I could not work these objects nor stick. I tried foreach, for and nothing.

I send the data to this PHP page via AJAX:

function insereVenda() {
        var vendafinal = [];
        var linhas = $("tbody>tr");
        var vendedor = $("#vendedor").val();
        var totalvenda = $("#totalvendas").text();

        linhas.each(function () {
            var produto = $(this).find("td:nth-child(1)").text();
            var quantidade = $(this).find("td:nth-child(2)").text();
            var valor = $(this).find("td:nth-child(3)").text();


            var sale = {
                vendaproduto: produto,
                vendaqtd: quantidade,
                vendavalor: valor

            };

             vendafinal.push(sale);


        });


        var dados = {
            vendafinal: vendafinal,
            vendedor: vendedor,
            totalvenda: totalvenda
        };

        $.post("salvarvendabkp.php", dados, function (retorna) {
            $("#retorna1").html(retorna);
        });
    }
  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

1 answer

1

Erik,

Since you are using a multidimensional array, you need to use a foreach (for example) to access the products and save them in the database.

Follow the example:

$vendafinal = $_POST['vendafinal'];
$venda_id = 123; // esse seria o id do registro que você acabou de salvar na primeira tabela

foreach($vendafinal as $produto) {
    salvarProduto($produto['vendaproduto'], $produto['vendaqtd'], $produto['vendavalor'], $venda_id);
}

function salvarProduto($nome, $quantidade, $valor, $venda_id)
{
    // aqui você faz as validações necessárias e salva no banco de dados
}

In the example of the Array you placed, the quantity and value are in the format of string. Make sure this is not checked in any data validation.

Browser other questions tagged

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