Jump to content
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.