How to capture variable SESSION PHP with JQUERY

0

This variable is in a .php view

<?php $actualizar=$_SESSION["actualizar"]; ?>

This is in a .js file is working under the mvc model

$( document ).ready(function() {
// the "href" attribute of the modal trigger must specify the modal ID that wants to be triggered
            $('#modal2').modal({
        dismissible: false, // Modal can be dismissed by clicking outside of the modal
        inDuration: 3000, // Transition in duration
        outDuration: 200, // Transition out duration
        startingTop: '4%', // Starting top style attribute
        endingTop: '10%', // Ending top style attribute
      });

            $('#modal2').modal('open');
    });

Any ideas on how to capture that variable session in jquery?

    
asked by Alberto Julio Arce Escolar 15.05.2018 в 19:18
source

1 answer

0

Do you have the possibility to save it in a cookie instead of a session? Sessions can not be obtained directly with JS but the cookies are.

Having:

<?php
    $actualizar = 'cookie de ejemplo'; // Valor de la cookie
    setcookie('actualizar', $actualizar, time() + 3600); // Crear cookie de 1h (3600s)
?>

In JavaScript you can get $_COOKIE['actualizar'] like this:

function getCookie(name) {
    let value = '; ' + document.cookie;
    let parts = value.split('; ' + name + '=');

    if (parts.length == 2) {
        return parts.pop().split(';').shift();
    }
}

let cookie_actualizar = getCookie('actualizar');
console.log(cookie_actualizar); // Imprime en consola: "cookie de ejemplo"
    
answered by 15.05.2018 / 21:29
source