How to put PHP session in IF ELSE condition inside DataTables?

How to put PHP session in IF ELSE condition inside DataTables?

mastersusemastersuse Posts: 61Questions: 28Answers: 0

How if I want to use PHP session inside DataTables using IF ELSE statement?
I already put <?php session_start(); ?> at PHP file. For my case, I want to hide edit_project() function from normal user level to access it.

This is my original that is working.

render: function (data) {
    return  "<span title='View Project' onclick='view_project(&quot;"+data+"&quot;)'></span>&nbsp;"+
            "<span title='Edit Project' onclick='edit_project(&quot;"+data+"&quot;)'>"+
            "<span title='Delete Project' onclick='delete_project(&quot;"+data+"&quot;)'></span>";
}

I try this but not working.

render: function (data) {
    var viewP = "<span title='View Project2' onclick='view_project(&quot;"+data+"&quot;)'></span>&nbsp;";
    var editP = "<span title='Edit Project2' onclick='edit_project(&quot;"+data+"&quot;)'></span>&nbsp;";
    var deleteP = "<span title='Delete Project2' onclick='delete_project(&quot;"+data+"&quot;)'></span>";
    if ("<?php if ($_SESSION['user_privilege'] == 'Admin'){ ?>"){
        return viewP + editP + deleteP + ("<?php } ?>");
    }
    else {
        return viewP;
    }     
}

and also I try this also not working.

render: function (data) {
    return  "<span title='View Project' onclick='view_project(&quot;"+data+"&quot;)'></span>&nbsp;"+
            "<?php if ($_SESSION['user_privilege'] == 'Admin'){ ?><span title='Edit Project' onclick='edit_project(&quot;"+data+"&quot;)'><?php } ?>"+
            "<span title='Delete Project' onclick='delete_project(&quot;"+data+"&quot;)'></span>";
}

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    You're mixing your PHP with your JS - you can't run PHP in the browser, so that line 5 in the second code block wouldn't work, as you said.

    Colin

  • mastersusemastersuse Posts: 61Questions: 28Answers: 0

    Do you any example solution for my case?

This discussion has been closed.