Use data from a $ _SESSION in Javascript?

0

Talk to everyone, okay?

I'm catching up with something that I believe should be something simple. I have this little function in javascript where according to the user level I should send to different pages.

Example, if the session user is level 1, you must do POST for search_report2.php, if the user is level 3 send to search_report3.php.

With the function I did, it ends up sending the two pros and the page is fighting with each 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 my $_SESSION it shows the logged in user and his level.  This part of my code is in a header.php file that contains the entire initial structure of my pages. I figured that since I already have session_start() set at the top of the page I could use this command. Is it over-mixing (because php is server-side and javascript client-side)?

Thanks my dear ones. Have a nice day =)

    
asked by anonymous 02.11.2017 / 17:39

1 answer

1

The fact that both if loops are running is a mismatch between the assignment operator ( = ) and the comparison operator ( == ). Then just correct for:

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

I'm 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
}

Then, in ajax requests, just call the file busca_relatorio.php , which will decide what should be returned.

    
02.11.2017 / 18:07