2

I have a page that allow users to access it. This page have certain things that not allow user to see that particular thing, For my case, user is unable to edit edit() or view this function, so I want to hide it by putting below code in JS because I am using function on this link.

I already enabled Session in PHP page like this <?php session_start(); ?>

And I try this putting this PHP in JS inside Datatables function. The dataTable function is working fine. Just the PHP is not working.

{ data : "project_id",
    render: function(data) {
    return  "<span onclick='view()'></span>&nbsp;"+    // This is allow to user and admin level
            "<?php 
              if ($_SESSION['user_privilege'] == 'Admin') { ?>
                 <span onclick='edit()'</span>
             <?php } ?>";   // This is allow for admin level only
    }
}
7
  • what do you mean the PHP is not working? Please be specific as possible Commented Apr 3, 2020 at 9:13
  • it mean user level still can see.view view(), should be not. I have two <span>. Commented Apr 3, 2020 at 9:19
  • maybe you can reply at the answer section, I don't get it Commented Apr 3, 2020 at 9:24
  • 1
    do a var_dump($_SESSION);. What does it give you? Commented Apr 3, 2020 at 9:28
  • sorry, typo, user level only can see view(), user level should be not to see edit() Commented Apr 3, 2020 at 9:30

1 Answer 1

6

Check the following

<?php
if(isset($_SESSION["user_user_privilege"])){
  echo "<script>
     let button = '<span onclick='edit()'></span>';
  </script>";
}else{
 echo "<script>
     let button = '';
  </script>";
}
?>

Now check the button is empty or not.

{ data : "project_id",
    render: function(data) {
    return  "<span onclick='view()'></span>&nbsp;"+  ((button!="") ? button : "");

    }
}
Sign up to request clarification or add additional context in comments.

14 Comments

Hi, I replicate your upper answer in .php file but have red underline at let button = "<span onclick='edit()'</span>";
I have made changes. check it now
$_SESSION["user_user_privilege"]) is just like that? or ($_SESSION['user_privilege'] == "Admin")
If you want to check $_SESSION["user_user_privilege"] is set or not, so pass the session into isset function. If you want to check session value so you can use condition like that ($_SESSION['user_privilege'] == "Admin")
[email protected], you can send on it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.