簡體   English   中英

顯示/隱藏具有會話的div(php)

[英]show/hide div with session (php)

我想在會話中隱藏div / button。 例如,如果會話用戶為“ abc”,則不想顯示div / button。 如果會話用戶為“ 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

我寫ajax事件的onclick事件:

$.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”時,我要隱藏1個按鈕,即3個按鈕。 當會話用戶為“ xyz”時,要顯示該按鈕。

首先檢查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.
} ?>

綁定onclick偵聽器時,需要檢查元素是否存在。 您可以這樣做:

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

查詢的length屬性給出查詢返回的元素數量,如果該值為0(因此沒有id estimate元素),則將跳過if。

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>";
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM