Access Javascript local variable and PHP variable stores

Asked

Viewed 41 times

-3

Good night!

How could I store the data of the variable "test" in "$variavelphp". As this code today the variable "test" is not accessible.

If it is not possible how I could access the value of the variable "test" in "teste2" in another part of the code.

                var ref = firebase.database().ref('Profissionais').orderByChild('Nome');


                //var u = 0;

        ref.on('child_added', function (snapshot) {
            var profissional = snapshot.val();
            if (profissional.provider == "usuarios" || profissional.provider == "Facebook" || profissional.provider == "Firebase" || profissional.provider == "Usuarios") {
                 teste =  "<tr><td>" + snapshot.key + "</td><td>" + profissional.Nome + "</td><td>" + profissional.Email + "</td><td>" + profissional.provider + "</td><td><a href='usuario.php?id=" + snapshot.key + "' class='btn btn-round btn-fill btn-info'>Consultar</a></td></tr>";


            }



            });



</script>


    <?php
        $variavelphp = "<script>document.write(teste)</script>";

        ?>

<script type="text/javascript">

var teste2 = teste;

alert(teste2);



</script>

1 answer

-1

Good morning, my friend. I suggest, after taking the data from Firebase, you do a post/get to a PHP page and take the data of the request itself.

If it’s GET, it would be like this:

$variavel = $_GET['teste'];

If it is POST:

$variavel = $_POST['teste'];

This way, you can pass the Javascript information to PHP. But, only with requests: POST, GET, PUT, etc...

I hope I’ve helped.

  • Hello, young man. Welcome to Stackoverflow. That doesn’t answer your question. It seems that the author of the question is trying to access a Javascript variable by PHP, this is not possible and, as already indicated above in the comments, there are several answers on the site about the subject.

Browser other questions tagged

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