Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

alianware

Passar variáveis de JS para PHP

Recommended Posts

alianware

<html>
<head>
  <script type="text/javascript">
   function Mudarestado(el, valoramudar) {
 var iddd = valoramudar;
 var display = document.getElementById(el).style.display;
 if(display == "none")
  document.getElementById(el).style.display = 'block';
 else
  document.getElementById(el).style.display = 'none';
   }
  </script>
  <?php  $mostra = "<script>document.write(iddd)</script>"; ?>
</head>
<body>
 <div id="minhaDiv" style="display: none;">Conteudo: <?php echo $mostra; ?></div>
 <?php $variavel = 4; ?>
 <button type="button" onclick="Mudarestado('minhaDiv', '<?php echo $variavel; ?>')">Mostrar / Esconder</button>
</body>
</html>

Estou a tentar passar uma variável através da propriedade onclick na função para depois mostrar na div escondida. Não está a aparecer o valor, podem me ajudar?

Share this post


Link to post
Share on other sites
KTachyon

O PHP é executado no servidor. O Javascript é executado no browser. Quando a página chega ao browser, o PHP já executou. Só depois é que o Javascript executa.

Edited by KTachyon

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.