The way you have structured your HTML will not allow you to retrieve the data correctly. See, considering three products, you'll get something like:
<div>
<input type="text" name="TamanhoP[]" class="form-control" placeholder="Tamanho">
<input type="text" name="EstoqueProd[]" class="form-control pull-left" placeholder="Estoque">
<input type="checkbox" name="EncomendaProd[]" value="Sim" style="width: 20px">Encomenda
</div>
<div>
<input type="text" name="TamanhoP[]" class="form-control" placeholder="Tamanho">
<input type="text" name="EstoqueProd[]" class="form-control pull-left" placeholder="Estoque">
<input type="checkbox" name="EncomendaProd[]" value="Sim" style="width: 20px">Encomenda
</div>
<div>
<input type="text" name="TamanhoP[]" class="form-control" placeholder="Tamanho">
<input type="text" name="EstoqueProd[]" class="form-control pull-left" placeholder="Estoque">
<input type="checkbox" name="EncomendaProd[]" value="Sim" style="width: 20px">Encomenda
</div>
Filling in as in the image provided in the question and making var_dump($_POST)
on the server that handles the form submission, we would have something like:
array(3) {
["TamanhoP"]=>
array(3) {
[0]=>
string(2) "32"
[1]=>
string(2) "33"
[2]=>
string(2) "34"
}
["EstoqueProd"]=>
array(3) {
[0]=>
string(2) "11"
[1]=>
string(2) "12"
[2]=>
string(2) "13"
}
["EncomendaProd"]=>
array(2) {
[0]=>
string(3) "Sim"
[1]=>
string(3) "Sim"
}
}
Notice that the array EncomendaProd
comes with only two values, referring to the first and third product on the form. This was already expected, but the problem is that we can not know what products were selected. It could be (1, 2), (1, 3) and (2, 3) that the request would be exactly the same. What you should do is differentiate each product from others. You can do this by indicating the index of each product in the field name, by setting the field name itself as a secondary index. For example:
<div>
<input type="text" name="produtos[0][tamanho]" class="form-control" placeholder="Tamanho">
<input type="text" name="produtos[0][estoque]" class="form-control pull-left" placeholder="Estoque">
<input type="checkbox" name="produtos[0][encomenda]" value="Sim" style="width: 20px">Encomenda
</div>
<div>
<input type="text" name="produtos[1][tamanho]" class="form-control" placeholder="Tamanho">
<input type="text" name="produtos[1][estoque]" class="form-control pull-left" placeholder="Estoque">
<input type="checkbox" name="produtos[1][encomenda]" value="Sim" style="width: 20px">Encomenda
</div>
<div>
<input type="text" name="produtos[2][tamanho]" class="form-control" placeholder="Tamanho">
<input type="text" name="produtos[2][estoque]" class="form-control pull-left" placeholder="Estoque">
<input type="checkbox" name="produtos[2][encomenda]" value="Sim" style="width: 20px">Encomenda
</div>
Note that for product 1, each field is defined as a secondary index in produtos[0]
. The same for the second product as% with% and for the third product as% with%. So, when submitting the form, we would have the following result for produtos[1]
:
array(1) {
["produtos"]=>
array(3) {
[0]=>
array(3) {
["tamanho"]=>
string(2) "32"
["estoque"]=>
string(2) "11"
["encomenda"]=>
string(3) "Sim"
}
[1]=>
array(2) {
["tamanho"]=>
string(2) "33"
["estoque"]=>
string(2) "12"
}
[2]=>
array(3) {
["tamanho"]=>
string(2) "34"
["estoque"]=>
string(2) "13"
["encomenda"]=>
string(3) "Sim"
}
}
}
Notice that for product 2, the produtos[2]
index will not be set, indicating that it has not been selected. To get the data, just iterate over the array var_dump($_POST)
:
foreach ($_POST["produtos"] as $produto) {
$tamanho = $produto["tamanho"];
$estoque = $produto["estoque"];
$encomenda = $produto["encomenda"] ?? "Não";
echo $tamanho, $estoque, $encomenda, "<br>";
}
Remembering that the null coalescing operator ( encomenda
) was only inserted in PHP 7. For previous versions you will need to do: $_POST["produtos"]
.
The output would be something like:
32 11 Sim
33 12 Não
34 13 Sim