Foreach does not carry marked fields

Asked

Viewed 57 times

1

Colleagues.

I have the form below where I put the size, stock and if the product is to order. If it is by order, just select the product according to the image below: inserir a descrição da imagem aqui

The size and the stock I can normally take the values, but the Order field only brings me the first value. See the code:

<label class="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 taking it this way:

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

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

The class of methods:

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]."');");
     }
  }   
.....
}

UPDATED POST

I changed the following line:

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

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

inserir a descrição da imagem aqui

That is, Idestoques 6 had to receive the value Yes and not Idestoques 5

  • Make a var_dump of the variable $_POST to see exactly what is happening.

  • Hello Kadu. I found that the problem is really in Jquery and not in PHP, because when I created the checkbox field manually, I was able to get the data, but when I put it back as in the post, only passes the first value.

  • Testing by changing the line novoCampo.find("input").val(""); for novoCampo.find('input[type="text"]').val(""); and then add the following novoCampo.find('input[type="checkbox"]').prop('selected', false);, Check if the problem persists.. Do you send the data via ajax? If it is, post the ajax code as well...

  • Hello Kaduamaral. I did as you requested, I was able to get the values, but different from what occurred in the alteration of my post, he now registers in the first two fields, even leaving marked as the image of my post.

1 answer

1


You’re clearing the value of checkbox with the line

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

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

novoCampo.find('input[type="text"]').val(""); 
novoCampo.find('input[type="checkbox"]').prop('checked', false);
  • Hi, Kadu. All right. I did as you said, I was able to get the values, but in the database it registers sequentially and not as selected in the image.

  • @Fox.11 ai we have already entered the question of logic. For example, fields of the type checkbox when they are not marked, they are not sent to the post. So you have to check the logic of your if($estoque[$i] != ""). I suggest opening a new question in case I can’t solve this.

  • Oh yes, not send via ajax.

  • Combined. Your answer helped me pick up the figures and was the solution to my problem here in the post. Thank you.

Browser other questions tagged

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