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:
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:
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.– KaduAmaral
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.
– user24136
Testing by changing the line
novoCampo.find("input").val("");
fornovoCampo.find('input[type="text"]').val("");
and then add the followingnovoCampo.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...– KaduAmaral
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.
– user24136