I have a web page about animals and veterinarians that I need to capture from a list an animal identifier (cd_pet) of the database and use it on another page when clicked, using the PHP language to provide the same information of the animal in this another page. The problem is that by using a while to cycle through all the animals in the database to fetch the animal's information, I can not click on the selected animal in the list and capture the identifier (cd_pet) of the selected animal and put it in a PHP variable.
The code used is this:
PHP / HTML
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$cd_usuario = $row['cd_usuario'];
if ($cd_usuario == $ID) {
?>
<li><a href=<?php echo "Perfil_de_usuario/".$row['nm_pet'].$row['cd_ra'].".php"; ?> onclick="pegarRA('<?php echo $row['cd_pet'] ?> ')">
<span id="esconder"></span>
<table style="width:80%; border: 1px solid black" >
<?php echo
" <tr><td class = 'ra' id='ra' for='feed-padrao'> <b>RA:</b> " . $row["cd_ra"].
" </td></tr><tr><td><b>Nome do Pet: </b>" . $row["nm_pet"].
" </td></tr><tr><td><b>Genero: </b>" . $row["ic_genero"].
" </td></tr><tr><td><b> Data Nascimento: </b>" . $row["dt_nascimento"].
" </td></tr><tr><td><b>Tipo de animal: </b>" . $row["nm_tipo_animal"].
" </td></tr><tr><td><b>Raça: </b>" . $row["nm_raca"].
" </td></tr><tr><td><b>Cor: </b>" . $row["ds_cor"].
" </td></tr><tr><td><b>Comportamento: </b>" . $row["ds_comportamento"].
" </td></tr><tr><td><b>Nome do Proprietario: </b>" . $row["nm_usuario"].
" </td></tr><tr><td><b>Email do Proprietario: </b>" . $row["cd_email"]."</td></tr><br>";
?>
JavaScript
<script>
//TESTE PARA CAPTURAR O IDENTIFICADOR
function pegarRA(cd_pet){
var cdEscondido = cd_pet
document.getElementById("esconder").innerHTML = cdEscondido;
document.getElementById("esconder").style.visibility = "hidden";
alert(cdEscondido);
}
//FUNÇÃO PARA DEIXAR A LISTA DE BUSCA AUTOMÁTICA
function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}