检查会话值(ajax)

时间:2015-08-15 15:47:52

标签: javascript php html ajax

我正在建立一个网站,但我想检查一个会话的价值,这样我就可以根据用户角色显示一个按钮。

我遇到的问题是我无法将会话的值带入SessionValue

我还要补充说该文件是.js

到目前为止:

function getGroups() {
var SessionValue = <?php print($_SESSION['user_role']); ?>;

$.ajax({
    url: 'services/functions.php',
    data: {
        action: 'getGroups'
    },
    type: 'post',
    success: function (output) {
        var result = JSON.parse(output);
        result = result.results;
        $(result).each(function (index, element) {

            var out = "<div class='row'>";
            out += "<div class='col-md-3'>";
            out += "<img class='img-responsive image_" + index + "' src=" + element.group_image + ">";
            out += "</div>";
            out += "<div class='col-md-9'>";
            out += "<h3>" + element.group_title + "</h3>";
            if(SessionValue == "admin"){
            out +=  "<button class='btn btn-primary pull-right' type='button' id='submitPost'>Delete</button>";
            }

            out += "<h4 class='hidden_" + index + "'>" + moment(element.group_playdate, 'HH:mm:ss').format('HH:mm') + "</h4>";
            out += "<p class='hidden_" + index + "'>" + element.group_description + "</p>";
            out += "</div>";
            out += "</div>";
            out += "<hr/>";

            $(".groupContainer").append(out);
            $(".hidden_" + index).hide();

            $(".image_" + index).hover(
                function () {
                    $('.hidden_' + index).fadeIn(200);
                },
                function () {
                    $('.hidden_' + index).fadeOut(100);
                }
            );
        });
    },
    error: function (output) {
        console.error("Something went wrong while loading the groups from the database.");
    }
});
}

会话已启动,user_role也已定义为@login。

0 个答案:

没有答案