If I understand the question correctly, you will have a table with several lines, and each row will have a checkbox, with the same name
, which will be checked by clicking the button of the same line.
In this case you should use the appropriate type for this, ie the radio
and not checkbox
. A set of type="radio"
with the same name
only allows 1 of them to be checked.
In this case you will not need the label
, since the radio will be checked via button. Another thing that becomes useless is the id="my_checkbox"
, that you can convert into class
and hide the radio via CSS:
<input type="radio" class="form-check-input my_checkbox" name="os_codigo1" value="<?php echo utf8_encode($dados['ped_codigo'])?>">
CSS:
.my_checkbox{
display: none;
}
It is also necessary to put the type
of buttons as button
and not as submit
:
<button class="btn btn-primary" type="button"><i class="fas fa-search"></i></button>
↑
Via Javascript you can check the radio from the same line as the button with:
document.addEventListener("DOMContentLoaded", function(){
// seleciona todos os botões
var botoes = document.querySelectorAll("td button.btn[type=button]");
// faz um laço percorrendo os botões adicionando um evento de click
for(var x=0; x<botoes.length; x++){
botoes[x].onclick = function(){
// busca o respectivo radio na mesma linha e checa ele
this.closest("tr").querySelector("[type=radio]").checked = true;
}
}
});
Example:
document.addEventListener("DOMContentLoaded", function(){
var botoes = document.querySelectorAll("td button.btn[type=button]");
for(var x=0; x<botoes.length; x++){
botoes[x].onclick = function(){
this.closest("tr").querySelector("[type=radio]").checked = true;
}
}
});
.my_checkbox{
/*display: none;*/
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
Deixei os radios visíveis apenas como exemplo para visualização:
<table>
<tr>
<td>2 dias</td>
<td><button class="btn btn-primary" type="button"><i class="fas fa-search"></i></button></td>
<td>
<div class="form-check">
<input type="radio" class="form-check-input my_checkbox" name="os_codigo1" value="2">
</div>
</td>
</tr>
<tr>
<td>3 dias</td>
<td><button class="btn btn-primary" type="button"><i class="fas fa-search"></i></button></td>
<td>
<div class="form-check">
<input type="radio" class="form-check-input my_checkbox" name="os_codigo1" value="3">
</div>
</td>
</tr>
</table>
If the checkbox is going to stay hidden, how will you know that it was triggered?
– Sam
You want to be submitted only 1 checkbox or can be several that have been triggered?
– Sam
Your question is a bit confusing. If it is a repetition structure there will be several checkboxes on the screen at the same time as it seems to me. Also, I noticed that you have in your code a Label, but the for="" of it is for an ID that does not exist, because the input:checkbox above has an ID with a name other than related to Label...
– hugocsl
@hugocsl Really mt confused. But I believe he wants to remove the label, since he wants the checkbox to be triggered by the button
– Sam
Sorry it took me so long to get the answer. I have already made a repeating structure that creates several checkboxes and when I select determining table field(This is all within a table) I send for $_SESSION[''] and the checkbox serves to differentiate the various Insert that the field 'code' has. I have a button with "magnifying glass" icon and it submits the checkbox, but just like it there are several of these buttons for each line of Insert. I would like the button to trigger the checkbox that would have <pre>display: None;</pre> or another way to hide it. Sorry for the confusion.
– Emilio Saymon