A problem with variables in JS and PHP

Asked

Viewed 54 times

0

I’m building a book registration system, it’s quite easy actually, it only involves a little bit of PHP and Mysql, but my problem is time to display. See, it’s a lot of information, so I wanted to make a button that would hide the div of the book selected 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 adding more and more books, and each one has its own hide button, and here’s the problem: The first button hides itself, and the other’s buttons also hide the first, look at 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 community to help me solve this problem, and also apologise if I did something wrong in this post, it’s my first time in a forum.

  • Try it like this echo "Identificador: ".$linha['id']." <br />"; and it depends on your php parameter this declaration used, If you are trying to put php variable for javascript, it does not work well. Are different language.

  • It did not work ;-;. Even so, very sheltered for proposing a solution.

  • What are you using it for { } ? if it is tried for Angularjs?

  • Well, I figured that as the result of the query would display multiple results, one div was required for each book.

  • Right, if you are using correct Mysql, you need to use repetition to result from vector. Use mysql_fetch_array and follow the example http://php.net/manual/en/function.mysql-fetch-array.php

  • I am using: fetch(PDO::FETCH_ASSOC).

  • I understood and sorry or use Pdo, if this using PDO is different from php, put tags there PDO.

  • All right buddy! I appreciate your help and I’m sorry if I wasn’t specific in the tags.

Show 3 more comments

1 answer

2


The id (identifier) in html should be unique, so it will only work in the first div found, try to change 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 />';
  • Thanks for the cooperation, but now he’s not hiding anything. I think I’m putting some incorrect value in the variable $div, because it didn’t work :(

  • Inspect your page in the browser and see what is being written in the id of the div and if in the onclick function is calling the correct function (in your case hide) and is passing the id of the div correctly.

  • @Zenas updated the code, tries to see if it now works, maybe the value of $div can be string which impairs the execution of JS

  • Deborah, the div of the code up there is in conflict, because the original is Mudarestado, but I changed to Hide to facilitate the understanding of the code, I did not see that below also had to change. Thanks for the help :D

  • @flfonseca, how do you think I should declare the variable $div? At the moment it is: $div = "div_{$line['id']}"; Even after changing everything, the same thing is happening that I described there in the question. The entire code upstairs has been updated.

Browser other questions tagged

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