Foreach does not have marked fields

1

Colleagues.

I have the form below where I place the size, stock and if the product is to order. If it is to order, just select the product as shown below:

Thesizeandstockcanusuallypickupthevalues,buttheOrderfieldonlybringsmethefirstvalue.Seethecode:

<labelclass="checkbox-inline"><input type="checkbox" name="Encomenda[]" value="Sim" style="width: 20px">Encomenda</label>

JQuery that adds more fields:

<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.insertAfter("tr.linhas:last");
    removeCampo();
  });
});
</script>

I'm getting it like this:

$tamanho = $_POST["Tamanho"];
$estoque = $_POST["Estoque"];
$encomenda = $_POST["Encomenda"];

$metodos->cadastrar($tamanho,$estoque,$encomenda);

The method class:

public function cadastrar($tamanho,$estoque,$encomenda){
.....   
 for($i = 0; $i <= count($tamanho); $i++){
     if($estoque[$i] != ""){
        mysqli_query($this->conexao,"INSERT INTO tabela VALUES(null,'".$tamanho[$i]."','".$estoque[$i]."','".$encomenda[$i]."');");
     }
  }   
.....
}

POST UPDATED

I changed the following line:

novoCampo.find("[type=checkbox]").val("Sim");

I was able to get the values, but when playing in the database, according to the image above, it returns me as follows:

In other words, the IDEstoques 6 had to receive the value Yes and not the IDEstoques 5

    
asked by anonymous 23.05.2017 / 21:55

1 answer

1

You are clearing the checkbox value with the line

novoCampo.find("input").val("");

When checkbox you should change its state via prop and not its val value. Do the following:

novoCampo.find('input[type="text"]').val(""); 
novoCampo.find('input[type="checkbox"]').prop('checked', false);
    
23.05.2017 / 22:49