Jump to content

Passar variáveis de JS para PHP

Recommended Posts


  <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';
  document.getElementById(el).style.display = 'none';
  <?php  $mostra = "<script>document.write(iddd)</script>"; ?>
 <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>

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

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.

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.