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

1

Well, I have a PHP variable an index.php page. 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> 
    
asked by anonymous 09.03.2018 / 17:17

1 answer

0

very simple print your variable inside the script passing as a 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 pesoaluno.php you will receive the variable as follows

<?php
   echo $_GET['pegaAqui'];
?>
    
09.03.2018 / 17:24