A problem with variables in JS and PHP

0

I'm building a book registration system, it's quite easy actually, it only involves a bit of PHP and MySQL, but my problem is at the time of the display. See, it's a lot of information, so I wanted to make a button that would hide the selected book div with this method:

<script language="javascript" type="text/javascript">
function Mudarestado(el) {
var display = document.getElementById(el).style.display;
if(display == "none")
    document.getElementById(el).style.display = 'block';
else
    document.getElementById(el).style.display = 'none';
}
</script>
So far so good, he was hiding the div, but I started to add more and more books, and each one has its own hide button, and therein lies the problem: The first button hides itself, and the buttons of the others also hide the first, see the code:

$div = "div_{$linha['id']}";
// aqui eu mostro os valores de minha consulta
echo "Identificador:     {$linha['id']} <br />";
echo '<div id=".$div.">';
echo "Nome:              {$linha['nome']} <br />";
echo "Autor:             {$linha['autor']} <br />";
echo "Editora:           {$linha['editora']} <br />";
echo "Gênero:            {$linha['genero']} <br />";
echo "Volume:            {$linha['volume']} <br />";
echo "Ano:               {$linha['ano']} <br />";
echo "Número de Páginas: {$linha['numero_de_paginas']} <br />";
echo "Outros Dados:      {$linha['outros_dados']} <br />";
echo "</div>";
echo '<button type="button" onclick="Mudarestado(\'.$div.\')">Mostrar/Esconder</button><br /><br />';

I have tried to change the $ div variable, add it, print it on the screen to identify some error, it seems really impossible! I ask the collaboration of the community to help me solve this problem, and also excuses if I did something wrong in this post, it's my first time in a forum.

    
asked by anonymous 08.08.2016 / 20:56

1 answer

2

The id in the html should be unique, so it will only work on the first div found, try changing your code to the following:

// aqui eu mostro os valores de minha consulta
    echo "Identificador:     {$linha['id']} <br />";
    echo '<div id="div_{$linha['id']}.">';
    echo "Nome:              {$linha['nome']} <br />";
    echo "Autor:             {$linha['autor']} <br />";
    echo "Editora:           {$linha['editora']} <br />";
    echo "Gênero:            {$linha['genero']} <br />";
    echo "Volume:            {$linha['volume']} <br />";
    echo "Ano:               {$linha['ano']} <br />";
    echo "Número de Páginas: {$linha['numero_de_paginas']} <br />";
    echo "Outros Dados:      {$linha['outros_dados']} <br />";
    echo "</div>";
    echo '<button type="button" onclick="Mudarestado(\'div_{$linha['id']}.\')">Mostrar/Esconder</button><br /><br />';
    
08.08.2016 / 21:06