如何为PHP中的某些用户创建只读文本框?

时间:2022-12-28 20:47:08

I have a text box on my web page and I want to make it readonly for certain users in PHP. The following is the code of the textbox which is populated from an array retrieved from database.

我的网页上有一个文本框,我想让PHP中的某些用户只读它。以下是从数据库中检索的数组中填充的文本框的代码。

<td width="130" align="right" valign="top" nowrap>
    <label for="descr">Name</label>:
</td>
<td>
    <input type="text" name="descr" id="descr" value="<?php echo htmlentities($row_product['descr'],ENT_QUOTES,'UTF-8'); ?>" size="50">

I want only certain users to only read the value and not be able to change it.

我只希望某些用户只读取值而无法更改它。

3 个解决方案

#1


2  

textareas (and other inputs) have a readonly attribute. You can just put it in the tag for those users that meet the condition. XML compliant: readonly="readonly"

textareas(和其他输入)具有readonly属性。您可以将其放在符合条件的用户的标记中。符合XML:readonly =“readonly”

#2


1  

You may add the readonly attribute to the input element.

您可以将readonly属性添加到input元素。

However, be aware that some clients might try to change the value when submitting the form anyway, so you must do server-side validation.

但是,请注意,有些客户端可能会在提交表单时尝试更改值,因此您必须执行服务器端验证。

#3


0  

ok. so you know when users are read-only,

好。所以你知道用户何时是只读的,

so for those users before the close of the input tag add:

所以对于那些用户在输入标签关闭之前添加:

<?=($readonly==true)?'readonly':''?>

UPDATE should have set previously if the user is a readonly user or not with a variable suchas:

如果用户是只读用户或者没有变量,则UPDATE应先前设置:

$readonly = true;

or

要么

$readonly = false;

so your td would be:

所以你的td将是:

    <td width="130" align="right" valign="top" nowrap>
       <label for="descr">Name</label>:
       </td> 
       <td>
       <input type="text" name="descr" id="descr" 
        value="<?php echo htmlentities($row_product['descr'],ENT_QUOTES,'UTF-8'); ?>" 
        size="50" 
        <?=($readonly==true)?'readonly':''?> />

#1


2  

textareas (and other inputs) have a readonly attribute. You can just put it in the tag for those users that meet the condition. XML compliant: readonly="readonly"

textareas(和其他输入)具有readonly属性。您可以将其放在符合条件的用户的标记中。符合XML:readonly =“readonly”

#2


1  

You may add the readonly attribute to the input element.

您可以将readonly属性添加到input元素。

However, be aware that some clients might try to change the value when submitting the form anyway, so you must do server-side validation.

但是,请注意,有些客户端可能会在提交表单时尝试更改值,因此您必须执行服务器端验证。

#3


0  

ok. so you know when users are read-only,

好。所以你知道用户何时是只读的,

so for those users before the close of the input tag add:

所以对于那些用户在输入标签关闭之前添加:

<?=($readonly==true)?'readonly':''?>

UPDATE should have set previously if the user is a readonly user or not with a variable suchas:

如果用户是只读用户或者没有变量,则UPDATE应先前设置:

$readonly = true;

or

要么

$readonly = false;

so your td would be:

所以你的td将是:

    <td width="130" align="right" valign="top" nowrap>
       <label for="descr">Name</label>:
       </td> 
       <td>
       <input type="text" name="descr" id="descr" 
        value="<?php echo htmlentities($row_product['descr'],ENT_QUOTES,'UTF-8'); ?>" 
        size="50" 
        <?=($readonly==true)?'readonly':''?> />