How do I use the value of a Javascript variable in a PHP variable to later use this value on other pages of the site?

Asked

Viewed 155 times

0

I have a web page about animals and veterinarians that I need to capture from a list an animal identifier (cd_pet) from the database and use it on another page when clicked, using the PHP language to provide the same animal information on this other page. The problem is that using a while to go through all the animals in the database to get the information from the animal, I cannot click on the chosen animal from the list and capture the identifier (cd_pet) of the selected animal and put 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";

        }
    }
}

  • Do you comment on cd_pet but this value doesn’t even exist in the code, so it gets confused. What exactly do you want to do?

  • I cut the code so it doesn’t get too long.. cd_pet is auto_increment and is from the database... it is quoted inside onclick="pegRA()", which makes when I click on the list, the value of cd_pet is captured from the database and goes to the function that calls an Alert (Alert just used for testing)... but I only know how to use the value to use it on the same page after the pet has been selected in the list, however, I want to use it on another page.. and I can’t do it if it’s not in PHP.

  • But you want to redirect to another page and pick up the value on this or you need to make some asynchronous request for more record details?

  • I will simulate here... I am owner of the animal and I am on page A and I want to choose one of my pets to register or delete more information, information that only the pet owner can register or delete, due to permission... On page A, there is a list of some previous information (a summary) of the animal, such as pet name, breed, date of birth and so on, all in a table that, when I click, redirects me to page B, which is where it contains all Pet information and it is on this B page that I can register or delete the information.

  • An example of one of the things I intend to do, is the owner of the animal can delete the profile (all information) of the animal, on page B, only for this, he must choose which of his animals from the list He intends to access.

  • This code I posted is from page A, more precisely is the list of registered pets.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.