开发者

How can incorporate session variable in jQuery

开发者 https://www.devze.com 2023-02-23 01:35 出处:网络
I want to incorporate a session variable $_SESSION[\'LI\'] into the jQuery with an if statement. So if($_SESSION[\'LI\'] == \'falsus\') hide the #feedback else show #feedback.

I want to incorporate a session variable $_SESSION['LI'] into the jQuery with an if statement. So if($_SESSION['LI'] == 'falsus') hide the #feedback else show #feedback.

Thanks

<?php
session_start();
$_SESSION['LI'] = 'falsus';
?><html>
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            $('#feedback').hide();
        });
        </script>
    </head>
    <body>
        <div id="feedback">Hello</div>
        <? print_r($_SESSION);?>
    &l开发者_JS百科t;/body>
</html>


Could you just do something like this:

<?php
    session_start();
    $_SESSION['LI'] = 'falsus';
?>
<html>
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
        <?php if($_SESSION['LI'] == 'falsus'): ?>
            <script type="text/javascript">
                $(document).ready(function() {
                    $('#feedback').hide();
                });
            </script>
        <?php endif; ?>
    </head>
    <body>
        <div id="feedback">Hello</div>
        <? print_r($_SESSION);?>
    </body>
</html>

This will only add the script to hide the #feedback div when the session variable is "falsus". If it is NOT "falsus", then the whole script block is omitted.


<?php
session_start();
$_SESSION['LI'] = 'falsus';
?><html>
<head>
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
<?php if ($_SESSION['LI'] == 'falsus') { ?>
        $('#feedback').hide();
<?php } else { ?>
$('#feedback').show();
<?php } ?>
    });
    </script>
</head>
<body>
    <div id="feedback">Hello</div>
    <? print_r($_SESSION);?>
</body>
</html>

You can weave your code with php wherever you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号