Javascript 若php会话具有特殊值,则向元素添加只读属性

Javascript 若php会话具有特殊值,则向元素添加只读属性,javascript,php,variables,Javascript,Php,Variables,我需要根据登录的用户隐藏或设置两个输入字段。如果某个特定用户已登录,则会显示该用户,其他用户则不会显示。我可以让第一部分工作,并找到正确的用户。在本例中,我试图使它们成为只读的 $user = $_SESSION['user_name']; 然后我尝试各种IF语句时有点迷路了 if ($user == 'Jim') { document.getElementById("notes").readonly = true; document.getElementById("coin

我需要根据登录的用户隐藏或设置两个输入字段。如果某个特定用户已登录,则会显示该用户,其他用户则不会显示。我可以让第一部分工作,并找到正确的用户。在本例中,我试图使它们成为
只读的

$user = $_SESSION['user_name'];
然后我尝试各种IF语句时有点迷路了

if ($user == 'Jim') { 
    document.getElementById("notes").readonly = true;
    document.getElementById("coins").readonly = true;
}
这个IF语句是什么导致了我认为什么都不起作用

<tr>
    <td><b>OFFICE USE:-</b></td>
    <td><b>Notes & Coins</b></td>
</tr> 
<tr>
    <td>Notes:-</td>
    <td><input type="text" id="notes"></td>
</tr>
<tr>
    <td>Coins:-</td>
    <td><input type="text" id="coins"></td>
</tr>

办公室用途:-
纸币和硬币
注:-
硬币:-

您的条件代码不正确,因为您无法将所显示的javascript代码与php代码相结合。javascript代码应该包装在

您正在将php(
$user
)与javascript(
document.getElementById…
)混合使用。根本不需要使用javascript:

if ($user == 'Jim') { 
   $readonly = "readonly";
} else {
   $readonly = "";
}
// then, where you need an input to be readonly do:
?>
<input type="text" id="notes" <?php echo $readonly ?>>
if($user=='Jim'){
$readonly=“readonly”;
}否则{
$readonly=“”;
}
//然后,如果需要输入为只读,请执行以下操作:
?>

您正在将php与javascript混合使用。
<?php $user = $_SESSION['user_name'] ?>
<tr>
  <td><b>OFFICE USE:-</b></td>
  <td><b>Notes & Coins</b></td>
</tr>
<tr>
  <td>Notes:-</td>
  <td><input type="text" id="notes" <?php $user=='Jim' ? echo 'readonly' : ''?>></td>
</tr>
<tr>
  <td>Coins:-</td>
  <td><input type="text" id="coins" <?php $user=='Jim' ? echo 'readonly' : ''?>></td>
</tr>
if ($user == 'Jim') { 
   $readonly = "readonly";
} else {
   $readonly = "";
}
// then, where you need an input to be readonly do:
?>
<input type="text" id="notes" <?php echo $readonly ?>>