So I have a history on my site and I want to get the values from the clicked row and show them only for that row id. I have table but when I click in each row it shows the same values for different rows like
HTML code:
<table style="width:100%" id="listagem">
<thead>
<tr>
<th style="font-family: Arial; font-size: 20px;">Data</th>
<th style="font-family: Arial; font-size: 20px;">Sala</th>
<th style="font-family: Arial; font-size: 20px;">Tipo</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($conn, "SELECT * FROM `ocorrencia` as o ORDER BY
o.data DESC");
while($row = mysqli_fetch_assoc($result)): ?>
<tr id="tr" onclick="hide(), window.location = '#bg'">
<td idlista="<?php echo $row['id']; ?>" style="display:visible;font-family:
Arial; font-size: 12px;"><a><?php echo $row['id']; ?></a></td>
<td style="font-family: Arial; font-size: 17px;"><?php echo $row['data']; ?>
</td>
<td style="font-family: Arial; font-size: 17px;"><?php echo $row['sala']; ?>
</td>
<td style="font-family: Arial; font-size: 17px;">
<?php
$result_t = mysqli_query($conn, "SELECT m.tipo
FROM `material` as m
INNER JOIN `ocorrencia_detalhe` as od on m.id = od.id_tipo
AND od.id_ocorrencia=".$row['id']);
while ($row2 = mysqli_fetch_assoc($result_t)): ?>
<?php echo $row2['tipo'] . " "; ?>
<?php endwhile;?>
</td>
</tr>
<?php endwhile; ?>
Here is the js code:
var idocorrencia;
$(document).on("click","#listagem tr", function(e){
e.preventDefault();
idocorrencia = $(this).parent().attr("idlista");
$("#listagem caption").text($(this).text());
$.post( "historico.php", { idoc: idocorrencia })
});
And here is the php code:
<?php
print_r($_POST);
$idoc = isset($_POST['idoc']) ? $_POST['idoc'] : 1;
echo $idoc;
$query = "SELECT * FROM ocorrencia where id=$idoc";
echo $query;
$result1 = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result1)): ?>
<p class="data"><?php echo $row['data']; ?></p>
<p class="sala"><?php echo $row['sala']; ?></p>
<textarea readonly id="descricao"><?php echo $row['descricao'];?></textarea>
<?php endwhile; ?>