I need to play a php variable in javascript

Asked

Viewed 157 times

0

I have a code where I need to put inside a javascript a variable that is the ID of the person I’m pulling, but I can’t find the solution. Where am I going wrong?

<?php echo "
                <tr>
                <td>".$row['nome']."</td>
                <td>".$row['telefone']."</td>
                <td>".$idade."</td>
                <td>".$row['cidade']."</td>
                <td>".$row['cargo_pretendido']."</td>

                <td><button onclick='mostrar()' class='w3-button w3-black'>INFOk123</button><td>
                </tr>

                <div id='id\"$row['id']\"' class='w3-modal'>
                    <div class='w3-modal-content'>
                        <header class='w3-container w3-teal'>
                            <span onclick='sumir()' class='w3-button w3-display-topright'>&times;</span>
                        </header>
                        <p>Teste</p>
                    </div>
                </div>

          ";
    }
    echo "<br>";

}


?>

<script language="javascript" type="text/javascript">


    function mostrar() { 
       document.getElementById('id"<?php echo $row['id']; ?>"').style.display='block'; 
    }

    function sumir() { 
       document.getElementById('id"<?php echo $row['id']; ?>"').style.display='none'; 
    }

</script>
  • Here: document.getElementById('id<?php echo $row['id']; ?>').style.display='block'; and here: <div id='id".$row['id']."' class='w3-modal'>

1 answer

0


As you are receiving the ID dynamically, the ideal would be to receive the ID in the functions:

//Receber o ID nas funções
<script language="javascript" type="text/javascript">

function mostrar(id) { 
   document.getElementById(id).style.display='block'; 
}

function sumir(id) { 
   document.getElementById(id).style.display='none'; 
}

</script>

And pass the ID as parameter when generating html with php

<td><button onclick=\"mostrar('id".$row['id']."')\" class='w3-button w3-black'>INFOk123</button><td>
            </tr>

            <div id='id".$row['id']."' class='w3-modal'>
                <div class='w3-modal-content'>
                    <header class='w3-container w3-teal'>
                        <span onclick=\"sumir('id".$row['id']."')\" class='w3-button w3-display-topright'>&times;</span>
                    </header>
                    <p>Teste</p>
                </div>
            </div>
  • That answer worked, now I understand how it works! Thank you!

Browser other questions tagged

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