Problem when using a session variable of php codeigniter in a js file

0

As I put it in the question, how can I do to use a codeigniter session variable to a js file?

Now, there are certain things that should be known;

The first thing is: The session variables are in a file And where I want to use them is another file (.js).

The second thing is: I'm working the html and the Js in the same file.

and The Third is: That I am organizing my code and I am placing the files php, html, js c / u where it corresponds, that is, the js in your js folder, the html in your html folder and so on .. ..

and it happens that I no longer recognize the session variables of php codeigniter because when moving the entire script to its corresponding file, they do not exist in these session variables.

In Summary: Will there be any way to use php session variables in a Js file?

- > Knowing that they are 2 different files.

Here I leave the Code as I currently have it (all in the same .php file):

<script>

    $(document).ready(function(){

var nom = "<?php print $this->session->userdata("sess_names"); ?>";
var ape = "<?php print $this->session->userdata("sess_apes"); ?>";
var ced = "<?php print $this->session->userdata("sess_cedu"); ?>";

...

</script>
    
asked by oarodriguez 16.10.2018 в 17:25
source

1 answer

0

once try the same thing you want to do and you could try to send it in the URL as a kind of GET method, but this implies in the following

You must send the variables in the Script invocation

<script type="text/javascript" src="/js/codigo.js?var1=var_sess_1;var2=var_sess_2"></script>

And using the command within the Script, get the URL:

localhost / js / code.js? var1 = value; var2 = value

var URLactual = window.location.href;
alert(URLactual);

And using a paro method with the .split function and thus separating the variables indicated after ? so assign them to variables inside the file.

But all this involves a lot of work and effort that sometimes is doubtful if it is worth doing.

A second option depending on what you wanted to do is use Ajax as you execute part of your code in the Js and then perform the server-side request (php) where you can use your variables from session.

Another simpler option for these cases is simply to create a file .php you place it in the same folder of the js and this only contains your js functions to which you send your variables

<script type="text/javascript">
    /* Codigo Java SCript */
</script>
    
answered by 16.10.2018 / 20:05
source