get variable value javascript

Asked

Viewed 457 times

-1

Hello! I need to take the value of a javascript variable, post to a php page to create a php Session variable. here I try to pass the variable js:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    var valor = "";
    $(document).ready(function() {
        $('input:radio[name=aviso]').click(function() {

            //Executa Loop entre todas as Radio buttons com o name de valor
            $('input:radio[name=aviso]').each(function() {
                //Verifica qual está selecionado
                if ($(this).is(':checked'))
                    valor = parseInt($(this).val());
            })

            alert(valor);
            $.post( "cria_session.php", { aviso: valor} );
        })
    })
</script>

Here I try to at least print on the screen:

<?            
        echo "Olá <script>document.write(valor)</script>"?>

Only what works is Alert(value);

Does anyone know?

  • You will get the value in PHP with $_POST['aviso']

  • I’ve tried that. But it didn’t show anything!

  • 1

    It will not display anything at all, because PHP will run on the server when Ajax requests. Unless you use a callback for some purpose.

  • In javascript, try to give a few console.log(valor); and see if the variable has any information.

  • Marcelo Martins, has yes. It is being displayed correctly in Alert(value);

  • So... Inside the js script it’s all ok. It’s just not sending the PHP pro value!

  • How do you know you’re not sending?

  • 'Cause I’ll have it printed $_POST['warning']

Show 3 more comments

2 answers

1

It is important to know that one of the features of Javascript is the Callbacks. (https://codeburst.io/javascript-what-the-heck-is-a-callback-aba4da2deced)

So, when you send the variable to the file cria_session.php, you can create the session with the value you sent, and at the end make the return of that value that will be shown in the Callback of the function that sends the values. Example of the file creat_session.php:

<?php
session_start();

$valor = $_POST['aviso'];
$_SESSION['aviso'] = $valor;

echo $valor;

In javascript, in $.post callback you do variable Alert. Example:

$.post("cria_session.php", {aviso: valor}, function(result){
    alert(result);
  });
  • The return PHP in Ajax has no effect. It would have to be echo.

  • As it has no effect ? Returns variables, objects, whatever you want. And with that you do what you want in the post’s callback.

  • Return will only work there in PHP, does not send anything back to Ajax. IE, the result of the callback will come empty.

  • You’re right, but if you print the variable the result is already filled.

  • No. PHP remains empty, although javascrip retrieves the value and only inside the script. Elsewhere on the page it does not print.

  • @Rubenssgarbiayres you need to explain better what you want to do and what your idea is.. Your question was to take a JS variable, send it to a PHP file and create a session, that’s answered. We don’t know the purpose of your application and what exactly you want to do with that variable to help you.

  • It’s answered, but it doesn’t work...

Show 2 more comments

0

Young lady, I got it here. I changed the end of the script. It looks like this:

<script type="text/javascript">

         $(document).ready(function() {
          $('input:radio[name=aviso]').click(function() {
            var valor = "";
                 //Executa Loop entre todas as Radio buttons com o name de valor
                 $('input:radio[name=aviso]').each(function() {
                     //Verifica qual está selecionado
                     if ($(this).is(':checked'))
                         valor = parseInt($(this).val());

                 })

                 alert(valor);
                 //document.write(valor);
                 $.post("cria_session.php", {
                    aviso : valor}, function(msg){
                    $("#resultado").html(msg);

})

             })
         })

     </script>

Browser other questions tagged

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