0
How do I pass a Javascript/Jquery variable to a Global Scope variable in PHP? recently I needed a global scope variable in PHP to receive a javascript value, this variable would be used later on other pages, I came to use Ajax and send the variable via POST to a php page and then include the result via append on the main page, only that I would need the PHP variable that received the value to be available globally to be used in other confirmations and pages. To get around the problem I passed the value via Cookie with Jquery, but this does not seem to be the right way to do this. the code below is trying to use Ajax to try to create the global scope variable in PHP
index php.
<script>
$(window).ready(function(){
var variavel = "valor a ser passado para variavel de escopo global php";
$.ajax({
method: "POST",
url: "script.php",
data: { nome: variavel }
})
.done(function( data ){
// resposta do servidor
$("#conteudo").append(data)
});
});
</script>
<div id='conteudo'></div>
php script.
Receives the Post JS value and is added via append on the main page
if( isset($_POST['nome'])){
$valor = $_POST['nome'];
$varTeste = json_encode($valor);
$varTeste; // variável que deveria ficar disponível globalmente
echo $varTeste;
}
Already studied PHP sessions?
– Woss
I hadn’t really thought of using sessions, and now that you spoke it was obvious that it was what I should have used, thank you for the reply, soon I update with the little code that solves the problem for some other lost soul rsrrsr, vlw msm Anderson
– Kleber Germano