Pass php variable by javascript and then pick up php via post

Asked

Viewed 31 times

1

Well, I have a PHP variable a page index.php. And when I click, I load another part of the page inside a div with javascript. When loading the page, the index variable is not recognized inside the div page (which is also . php).

<?php
$variavel = 1234;
?>


<script>
function carregar(pagina){
    $("#conteudo").load(pagina);
}
</script>



<ul class="nav nav-tabs">
<li class="nav-item">

<a class="nav-link active"   onclick="carregar('includes/graficos/pesoaluno.php')" href="#">Registro de peso</a>

</li>

</ul>

<div id="conteudo"></div> 
  • You can switch to a variable Javascript thus: var x = '<?php echo $variavel;?>';

1 answer

0


very simple print your variable inside the script passing as parameter $_GET

See the example below

<script>
function carregar(pagina){
    $("#conteudo").load(pagina+"?pegaAqui=<?php echo $variavel; ?>");
}
</script>

...

<a class="nav-link active"             onclick="carregar('includes/graficos/pesoaluno.php')" href="#">Registro de peso</a>

Now inside your page peoaluno.php you will receive the variable as follows

<?php
   echo $_GET['pegaAqui'];
?>
  • 1

    It worked. Thank you very much!

Browser other questions tagged

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