Using data from a $_SESSION in Javascript?

Asked

Viewed 1,345 times

0

Talk, you guys, all right?

I’m picking up something that I believe should be something simple. I have this little javascript function where according to the level of the user should send to different pages.

For example, if the Session user is level 1, he should do the POST for search_relation2.php, if the user is level 3 send to search_reportario3.php.

With the function I did he ends up sending to the two and the page keeps fighting using one and the other.

What I tried to do was this:

var nivel = <?php echo $_SESSION['nivel']; ?>;

    if(nivel = 1){
        $('#btn_buscar').click( function(){
            $.ajax({
                url:'inc/busca_relatorio2.php',
                method: 'post',
                data: $('#resultado_busca').serialize(),
                sucess: function(data){
                    $('#resultado_busca').val('');
                }
            });
        });
        $('#form_data').submit(function(e){
        e.preventDefault();
        $.ajax({
            url:'inc/busca_relatorio2.php',
            method: 'post',
            data: { busca_data: $('#busca_data').val()}, // sua data chegará como $_POST['busca_data'] no PHP.
            success: function(data){                                                        
                 return false; //não vai redirecionar a lugar algum
            }
        });                 
    });             
    }

    if(nivel = 3){
        $('#btn_buscar').click( function(){
            $.ajax({
                url:'inc/busca_relatorio3.php',
                method: 'post',
                data: $('#resultado_busca').serialize(),
                sucess: function(data){
                    $('#resultado_busca').val('');
                }
            });
        }); 
        $('#form_data').submit(function(e){
        e.preventDefault();
        $.ajax({
            url:'inc/busca_relatorio3.php',
            method: 'post',
            data: { busca_data: $('#busca_data').val()}, 
            success: function(data){ 
                 return false; //não vai redirecionar a lugar algum
            }
        });                 
    }

If I debug mine $_SESSION shows the user logged in and the level of it. That part of my code is in a file header.php that contains the entire initial structure of my pages. I imagined that as I already have the session_start() set at the beginning of the page could use this command. I’m mixing too much (because php is server-side and javascript client-side)?

Thank you, my friends. Have a good day =)

1 answer

1

The fact that both if links are running is a mistake between the assignment operator (=) and the comparison operator (==). So just fix it to:

...
...
if(nivel == 1){
...
...
...
if(nivel == 3){
...
...

Am I mixing too much (because php is server-side and javascript client-side)?

Yes. This validation could only be done with PHP. You could create a new file .php (busca_relatorio.php, for example). Getting more or less like this:

<?php
$nivel = $_SESSION['nivel'];
if($nivel == 1){
    //chame alguma função, inclua algum arquivo
}else if($nivel == 3){
    //chame alguma função, inclua algum arquivo
}

And then, in the ajax requests, just call the file busca_relatorio.php, that he will decide what is to be returned.

  • I ended up realizing that my problem is in another function where I return the functions, so even putting this filter will still have errors. But thanks anyway, I’ll better organize my code :)

Browser other questions tagged

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