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?

Link to comment
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

Link to comment
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
  • 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.