2012-10-24 14 views
-1

請幫助,我試圖讓用戶或訪客訪問我的網頁時,他們會看到一個禁用的文本區域,一旦他們登錄,他們就可以點擊編輯該文本區域。如果登錄/退出,顯示/隱藏textarea?

我已經非常設置了我想要它,但此刻如果用戶登錄文本區域從禁用的切換​​到可編輯的。但其他配置文件的所有其他文本區域消失,我希望這些留下來,登錄的用戶可以看到其他用戶,但不編輯它們?

希望我已經解釋清楚了。希望這可能嗎?

<?php if (isset($_SESSION['user_id'])) { 
      if ($user['id'] == $_SESSION['user_id']){ 
?>     

<textarea id="area" rows="10" style=" 
    width: 456px; 
    margin-top:3px; 
    text-align:left; 
    margin-left:-2px; 
    height: 122px; 
    resize: none; 
    border: hidden;"><?php echo $profile['bio'] ?> </textarea> 

<? 
} 
} 
?> 

<?php 
    if (!logged_in()) { 
     ?> 

<textarea id="area-read" disabled="yes" style=" 
    width: 456px; 
    margin-top:3px; 
    text-align:left; 
    margin-left:-2px; 
    height: 122px; 
    resize: none; 
    border: hidden;"><?php echo $profile['bio'] ?> </textarea> 
<? 
} 
?> 

回答

1

你說對於其他配置文件,textarea消失。這是因爲在用戶登錄後,他的會話被設置。並且只在id等於會話user_id的配置文件中,打印textarea。因此,您需要處理第一個if塊內的if的其他情況。

<?php 
if (isset($_SESSION['user_id'])) 
{ 
    if ($user['id'] == $_SESSION['user_id']) 
    { 
?>     

    <textarea id="area" rows="10" style=" 
    width: 456px; 
    margin-top:3px; 
    text-align:left; 
    margin-left:-2px; 
    height: 122px; 
    resize: none; 
    border: hidden;"><?php echo $profile['bio'] ?> </textarea> 

<?php 
    } 
    else 
    { 
    //Printing the text area for other users/profiles. This is what you said was missing. 
?> 

    <textarea id="area-read" disabled="yes" style=" 
    width: 456px; 
    margin-top:3px; 
    text-align:left; 
    margin-left:-2px; 
    height: 122px; 
    resize: none; 
    border: hidden;"><?php echo $profile['bio'] ?> </textarea> 

<?php 
    } 
} 
?> 

<?php 
    if (!logged_in()) 
    { 
?>    
    <textarea id="area-read" disabled="yes" style=" 
    width: 456px; 
    margin-top:3px; 
    text-align:left; 
    margin-left:-2px; 
    height: 122px; 
    resize: none; 
    border: hidden;"><?php echo $profile['bio'] ?> </textarea> 
<?php 
    } 
?> 
+0

謝謝:)這解決了它。 –

1

儘量把只有禁用=「是」在PHP檢查

<textarea id="area-read" <?php if(!logged_in()) { echo 'disabled="yes"'; } ?> 
+0

謝謝,但只是在登錄時重複可編輯文本區域,並顯示它們兩者。雖然它確實顯示所有其他用戶顯示爲可編輯文本區域:/ –

0

像Svetlio說,只有把殘疾人的檢查,同時也延長了支票,所以你只啓用textarea的此用戶並禁用其他用戶。

<textarea id="area-read" <?php if(!logged_in() || $_SESSION['user_id']!=$user['id']) { echo 'disabled="yes"'; } ?> 

編輯:

爲了應對您發表評論,通常你不會看到textarea的兩次登錄的用戶,所以這裏是一個什麼樣的代碼需要完整版本:

<textarea id="area-read" <?php if(!logged_in() || $_SESSION['user_id']!=$user['id']){ echo 'disabled="disabled"' } ?> style=" 
    width: 456px; 
    margin-top:3px; 
    text-align:left; 
    margin-left:-2px; 
    height: 122px; 
    resize: none; 
    border: hidden;"><?php echo $profile['bio'] ?> </textarea> 

這是你所需要的,僅此而已

+0

或者,如果您不喜歡單個檢查,則可以編輯最後一個if if(!logged_in()|| $ _SESSION ['' user_id']!= $ user ['id'])而不是 – Sonaryr

+0

多數民衆贊成在此非常感謝你的作品。所有的手機現在都顯示爲禁用,當用戶登錄時他/她可以編輯它們,但唯一的問題是禁用的文本區域和可編輯的文本區域現在都顯示爲登錄用戶。有沒有我可以用來隱藏殘疾人的一段代碼?謝謝。 –

+0

我編輯了迴應,通常只使用此代碼,您將看到每個用戶只有一個textarea,並且它將被禁用登錄用戶。 – Sonaryr

0

disabled="disabled"是textarea的正確的語法。 所以你應該:

<textarea id="area-read" <?php echo ((!logged_in() ? (" disabled='disabled' ") : ("")) ; ?>