【问题标题】:show/hide div with session (php)使用会话显示/隐藏 div (php)
【发布时间】:2015-12-19 02:37:15
【问题描述】:

我想在会话中隐藏 div/按钮。比如,如果会话用户是“abc”,那么就不想显示 div/按钮。如果会话用户是“xyz”,那么应该显示该 div/按钮。

我试过这个:

<?php if($_SESSION['usr'] == 'guest'){ ?>
        <input id="estimate" class="btn btn-sm btn-success " type="button" value="Get Estimation" />
<?php } ?>

我在那个标签上有一些 onclick 代码。

所以,它给了我这些错误:

无法将属性“onclick”设置为 null

Onclick 事件我写 ajax 事件:

$.ajax({
        type: "POST",
        url: "getsavedatalist.php",
        data: { uid: usrid },
        dataType: "html"
    })
    .done(function( msg ) {
        msg;
        document.getElementById('listCon').innerHTML = msg ;
    })
    .fail(function() {
        //alert( " error  in json request failed  " );
        alert( alt_getlist_arr );
    })
    .always(function() {

        console.log( "url :  " +  this.url );
    });

我有 3 个按钮。当会话用户是“abc”时,我想隐藏 3 个按钮的 1 个按钮。当会话用户是“xyz”时想要显示该按钮。!

【问题讨论】:

  • 显示绑定onclick事件处理程序的代码
  • 在此处添加您的完整代码,以便我们查看问题。
  • 该错误是否仅在按钮未显示时出现?
  • @Satpal 检查我对我的描述所做的编辑。
  • @KaushaMehta 检查我对我的描述所做的编辑

标签: php jquery html css session


【解决方案1】:

首先检查session_start(); 是否已添加到文件的第一行。然后输入下面的代码。

<?php if($_SESSION['usr'] == 'xyz'){
    //Put the buttons which you want to display on when the use is XYZ.
} else {
    //Put the buttons which you want to display on when the use is ABC.
} ?>

【讨论】:

  • 谢谢..!我已经使用了这些代码并做了一些技巧和它的工作。
【解决方案2】:

当你绑定你的 onclick 监听器时,你需要检查元素是否存在。你可以这样做:

if ( $( "#estimate" ).length ) {
    //Add your onclick here
}

查询的length属性给出了查询返回的元素数量,如果为0(所以没有id为estimate的元素)它将跳过if。

【讨论】:

    【解决方案3】:

    HTML 代码:

    <div class="form" id="xyz" style="display: none;">
    <div class="form" id="abc" style="display: none;">  
    

    PHP 代码:

    <?php 
        if ($_SESSION['usr'] == 'xyz') {
           $showdiv = 'xyz';
        }
        else if ($_SESSION['usr'] == 'abc') {
           $showdiv = 'abc';
        }
    
        echo "<script type=\"text/javascript\">document.getElementById('".$showdiv."').style.display = 'block';</script>";
    ?>
    

    【讨论】:

    • 如果涉及机密信息,那是非常糟糕的做法,这样您仍然可以提供内容,并且任何人都可以使用 javascript 控制台显示两个 div。但是,如果这无关紧要,那是肯定的。
    • 如果是机密信息,您可以通过检查会话即时创建 div,而无需隐藏 div 元素。
    • 是的,这正是他正在做的事情,当他尝试将点击侦听器附加到不存在的 div 时,他只是得到了错误。 (问题中的显示和隐藏是错误的术语,我认为这让你感到困惑)
    • 我从他的问题中了解到他需要显示和隐藏 --> “我想在会话中隐藏 div/按钮”
    • 检查,如果是这种情况,您的回答是否正确。 +1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2014-01-30
    • 1970-01-01
    相关资源
    最近更新 更多