Access a variable session from jquery

2

From jquery, I send to validate a user via Ajax, validation that is in a PHP. If the validation is Ok, the PHP puts the user in a session variable:

<?php
$_SESSION['nombre']=$usuario;

Then, I print said variable in the html:

<div>
<?php echo "Usuario Logueado:[".$_SESSION['nombre']." ]";?>         
</div>

Finally, from jquery via Ajax, I send information to record in mysql in another PHP. From this PHP, I try to access the session variable to record the user, but does not recognize it;

<?php
require_once ("funciones/conexiones.php");
$ven=$_SESSION['nombre'];

Reviewing in the console, throws:

Notice: Undefined variable: _SESSION

Why can not I access the value stored in the variable _SESSION?

Since now, thank you very much for the help.

Additionally, I tried to print the session variable in the jquery:

$(document).on('change', '.cmb_articulo', function () 
{
alert($_SESSION['nombre']);

but it throws me: ReferenceError: $ _SESSION is not defined

Thank you.

    
asked by Junco Fuerte 22.11.2016 в 04:13
source

2 answers

3

It seems that the problem is that you are not initiating the session. Add session_start to the beginning of your PHP files to start the session and then you should no longer receive that error and the session variable will pass well without using JavaScript (something that I would not recommend for what you are trying).

For example:

<?php
session_start();
require_once ("funciones/conexiones.php");
$ven=$_SESSION['nombre'];

Try that and tell me if you still have problems.

    
answered by 22.11.2016 / 04:23
source
0

You are mixing 2 things, the jquery and the php.

I do not know if it will work, but it should be something like this.

$(document).on('change', '.cmb_articulo', function () 
{
alert(<?=$_SESSION['nombre']?>);
    
answered by 05.02.2017 в 22:14