How to save two variables from a JS in php session?

Asked

Viewed 824 times

7

I would like to know if there is a way to save these two following variables. Follows the code:

<script type="text/javascript">
 function opcao()
        {
            $(document).ready(function() 
            {   
                var empresa = $('#Empresa option:selected').val();
                $('#Unidades').load('/site/funcoes2.php?emp='+ empresa);
            });
        }
        </script>
///////////////////////////////////////////////////////
 <script type="text/javascript">
     function uniopcao()
        {     
            $(document).ready(function () 
            { 
                var unid = $('#Unidades option:selected').val();
                $('#unn').load('/site/tpago.php?uni='+ unid);  
            });
        }
</script>

i would like to take these two variables ( variable "companies" that receives through an onchange the selected company) and a (variable "unid" that receives the chosen unit according to the chosen company.
I wanted to assign these values in a SESSION PHP as in the example below.

_SESSION['V1']=  empresa;
_SESSION['V2'] = unid;

I’m going to use that information throughout the rest of the program so I’d like to save it. Thank you.

2 answers

4


In your "funcoes2.php" file add the line:

$_SESSION['V1'] =  $_GET['emp'];

In your "tpago.php" file add the line:

$_SESSION['V2'] =  $_GET['uni'];

That way, whenever your ". load()" loads the url, the session will set the variables you want.

3

You will need to use Javascript to send the variable to a new link: pagina.php?empresa=1&uni=1

Then on that new page you use $_GET:

$_SESSION['V1']= $_GET["empresa"];
$_SESSION['V2']= $_GET["uni"];
  • thanks both answers it was valid thank you for the attention

Browser other questions tagged

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