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
    }
}
mastersuse
  • 936
  • 1
  • 15
  • 35

1 Answers1

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 : "");

    }
}
Hassan Nasir
  • 146
  • 5
  • Hi, I replicate your upper answer in .php file but have red underline at `let button = "";` – mastersuse Apr 03 '20 at 09:37
  • I have made changes. check it now – Hassan Nasir Apr 03 '20 at 09:44
  • `$_SESSION["user_user_privilege"])` is just like that? or `($_SESSION['user_privilege'] == "Admin")` – mastersuse Apr 03 '20 at 09:46
  • 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") – Hassan Nasir Apr 03 '20 at 09:50
  • i got this error `Uncaught ReferenceError: edit is not defined at HTMLSpanElement.onclick (home.php:1) onclick @ home.php:1` – mastersuse Apr 03 '20 at 09:51
  • Have you created edit() function. if not so please create edit() function – Hassan Nasir Apr 03 '20 at 09:52
  • yes already create and the function is working. modal is appear. On yes, the button is unseen. – mastersuse Apr 03 '20 at 09:56
  • May I know what is `((button!="") ? button : "");` use for? inside double quote it is need to put a condition? – mastersuse Apr 03 '20 at 10:39
  • button is variable created in php code. button value is , If session exists, then button value is , if session not exists so button value is empty. – Hassan Nasir Apr 03 '20 at 10:45
  • you upper and bottom solutions is required each other or choose either one? sorry I am new in php – mastersuse Apr 03 '20 at 11:10
  • Upper code is of php, that is generating the button variable, It checks if session is set then create variable with value of "span" tag, if session is not set, so button value is empty. – Hassan Nasir Apr 03 '20 at 11:27
  • do you have email? maybe I can share the actual issue? just a part yg the code for understanding.. sorry because I cannot relate to my case. – mastersuse Apr 03 '20 at 11:44
  • 2
    hassankwl1001@gmail.com, you can send on it. – Hassan Nasir Apr 03 '20 at 11:51
  • Hi thanks for your cooperation and kindness, I just got the answer, and it work successfully. Thanks again. – mastersuse Apr 03 '20 at 12:07