0
I have the following html:
<?php
$j=0;
while($rows_cursos1 = mysqli_fetch_array($resultado_cursos1)) {
?>
<tr>
<td style="display: none"><input type="text" name="NomeUtente[]" class="NomeUtente" value="<?php echo $rows_cursos1 ["codigo"]; ?>"></td>
<td style="font-size: 12px"><?php echo $rows_cursos1['nome']; ?></td>
<td style="display: none"><input type="text" name="Quarto[]" class="Quarto" value="<?php echo $rows_cursos1 ["quarto"]; ?>"></td>
<td style="font-size: 12px"><?php echo $rows_cursos1['quarto']; ?></td>
<td style="display: none"><input type="text" name="Cama[]" class="Cama" value="<?php echo $rows_cursos1 ["Cama"]; ?>"></td>
<td style="font-size: 12px"><?php echo $rows_cursos1['Cama']; ?></td>
<ul class="flex-outer">
<td style="font-size: 12px"><div class="form-check">
<label class="toggle">
<input type="checkbox" class="form-control" name="Miccao[<?php $j; ?>]"><span class="label-text"> Realizado</span> <input class="form-control" type="text" name= "Tipo1[]" class= "Tipo1">
</label>
</div></td>
<td style="font-size: 12px"><div class="form-check">
<label class="toggle">
<input type="checkbox" class="form-control" name="Dejeccao[<?php $j; ?>]"><span class="label-text"> Realizado</span> <input class="form-control" type="text" name= "Tipo[]" class= "Tipo">
</label>
</div></td>
</ul>
</tr>
<?php
$j++;
}
?>
Then send the data to the php page with the following function:
function inserir_registo26()
{
var NomeUtente = [];
$("input[name^='NomeUtente']").each(function() {NomeUtente.push(this.value)});
var Quarto = [];
$("input[name^='Quarto']").each(function() {Quarto.push(this.value)});
var Cama = [];
$("input[name^='Cama']").each(function() {Cama.push(this.value)});
var Miccao = [];
$("input[name^='Miccao']:checked").each(function() {Miccao.push(this.value)});
var Tipo1 = [];
$("input[name^='Tipo1']").each(function() {Tipo1.push(this.value)});
var Dejeccao = [];
$("input[name^='Dejeccao']:checked").each(function() {Dejeccao.push(this.value)});
var Tipo = [];
$("input[name^='Tipo']").each(function() {Tipo.push(this.value)});
var dadosajax = {
'NomeUtente[]' : NomeUtente,
'Quarto[]' : Quarto,
'Cama[]' : Cama,
'Miccao[]' : Miccao,
'Tipo1[]' : Tipo1,
'Dejeccao[]' : Dejeccao,
'Tipo[]' : Tipo
};
console.log(dadosajax);
$.ajax({
url: './registomiccao',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
$(".error_message").removeClass('hide');
},
success: function(result)
{
$('.form11')[0].reset();
Swal.fire('Boa!', 'Gravado com sucesso!', 'success');
}
});
php have it like this:
for ($i=0;$i<count($_POST["NomeUtente"]);$i++) {
$NomeUtente = $_POST["NomeUtente"][$i];
$Quarto = $_POST["Quarto"][$i];
$Cama = $_POST['Cama'][$i];
$miccao = $_POST["Miccao"][$i];
$Tipo1 = $_POST["Tipo1"][$i];
$dejeccao = $_POST["Dejeccao"][$i];
$Tipo = $_POST["Tipo"][$i];
$Colaborador = $_SESSION['usuarioId'];
$DataRegisto = date("Y-m-d H:i:s");
if( $miccao != ""){
$miccao1 = $miccao == "on" ? "Realizado" : "";
$query = 'INSERT INTO registoMiccao (`NomeUtente`, `Quarto`, `Cama`, `DataRegisto`, `Miccao`, `Tipo1`, `Colaborador`) VALUES ( ?, ?, ?, ?, ?, ?, ?)';
$stmt = $conn->prepare( $query );
$stmt->bind_param("sssssss", $NomeUtente, $Quarto, $Cama, $DataRegisto, $miccao1, $Tipo1, $Colaborador);
$stmt->execute();
}
if( $dejeccao != ""){
$dejeccao1 = $dejeccao == "on" ? "Realizado" : "";
$query1 = 'INSERT INTO registoDejeccao (`NomeUtente`, `Quarto`, `Cama`, `DataRegisto`, `Dejeccao`, `Tipo`, `Colaborador`) VALUES ( ?, ?, ?, ?, ?, ?, ?)';
$stmt1 = $conn->prepare( $query1 );
$stmt1->bind_param("sssssss", $NomeUtente, $Quarto, $Cama, $DataRegisto, $dejeccao1, $Tipo, $Colaborador);
$stmt1->execute();
}
}
When I’m about to enter, I should just enter the data from the lines I check the checkboxes on. But what’s happening is that if I check the checkbox on two lines, it inserts only two lines, but the data doesn’t belong to the lines where I checked the checkboxes.
For example if I check line 19 and 20 it inserts the data of line 1 and 2 and should enter the data of lines 19 and 20, because they were the ones I checked.
The problem is on these lines:
$("input[name^='Miccao']:checked").each(function() {Miccao.push(this.value)});
$("input[name^='Dejeccao']:checked")
Because when I send the arrays, in all variables it sends 46 lines and in the checkbox it only sends the number of arrays I select, as shown here.
{User[]: Array(46), Room[]: Array(46), Bed[]: Array(46), Miccao[]: Array(2), Type1[]: Array(46), ...}
but if you don’t put the :checked
in the two lines above it will bring everything as on
, as if they were checked