Get value unchecked with PHP

3

Colleagues.

I have a form that contains the following fields:

PleasenotethatIhaveselectedthefirstandthirdfieldsasanorder,butwhenIregisterinthedatabase,itreturnsmeasfollows:

WhenyouactuallyhadtobringmethenullID2andID3asYes.Totrytosolveit,Ididthis:

$valorEncomenda=($encomendaP[$i]==null)?("Não"):("Sim");

But it is registered this way:

Seethefullcodebelow:

FormFields:

<inputtype="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

JavaScript:

<script type="text/javascript">
$(function () {
  function removeCampo() {
    $(".removerCampo").unbind("click");
    $(".removerCampo").bind("click", function () {
       if($("tr.linhas").length > 1){
        $(this).parent().parent().remove();
       }
    });
  }

  $(".adicionarCampo").click(function () {
    novoCampo = $("tr.linhas:first").clone();
    //novoCampo.find("input").val("");
    novoCampo.find('input[type="text"]').val("");
    novoCampo.find('input[type="checkbox"]').prop('selected', false);
    novoCampo.insertAfter("tr.linhas:last");
    removeCampo();
  });
});
</script>

I get the form values:

$estoqueP = $_POST["EstoqueProd"];
$tamanhoP = $_POST["TamanhoP"]; 
$encomendaP = $_POST["EncomendaProd"];
$metodos->cadastrar($estoqueP,$tamanhoP,$encomendaP);

Sign in method ():

public function($idProdutos,$estoqueP,$tamanhoP,$encomendaP){

........

 if($estoqueP[0] != ""){
   for($i = 0; $i <= count($tamanho); $i++){
       if($estoqueP[$i] != ""){
          $valorEncomenda = ($encomendaP[$i] == null)?("Não"):("Sim");
           mysqli_query($this->conexao,"INSERT INTO tabela VALUES(null,'".$idProdutos."','".$tamanho[$i]."','".$estoqueP[$i]."','".$valorEncomenda."');");               
       }
   }
 }

........

}

How can I value unchecked?

    
asked by anonymous 23.05.2017 / 23:31

1 answer

2

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
    
24.05.2017 / 00:29