简体   繁体   English

保持表单字段不可编辑

[英]Keep form fields from being editable

I have a form that's pre-populated with values from a database. 我有一个预先填充有数据库值的表单。 If a user is logged in and has sufficient permissions, the user can edit the fields. 如果用户登录并具有足够的权限,则用户可以编辑字段。 That all works well; 一切正常; however, if the user is not logged in, the fields should not be able to be changed. 但是,如果用户没有登录,该字段应该不能够被改变。 I have it so that any changes the bad user makes won't save to the database, but I'd like to make it so that the user can't even click into those fields. 我这样做是为了使不良用户所做的任何更改都不会保存到数据库中,但是我想这样做,以便用户甚至无法单击这些字段。

Is there a way to conditionally allow form fields to be clickable? 有没有一种方法可以有条件地允许表单字段可单击?

只需将readonly属性添加到HTML标记中,如下所示:

<input type="text" value="sample" readonly>

如果用户未登录,则可以在输入中添加禁用标记。例如: <input type="text" name="lname" disabled>

You could disable the fields when no logged in. 您可以在没有登录时禁用这些字段。

<input type="text" name="lname" <?php print (isLoggedIn($user)?"disabled":"");?> >

if, for whatever reason, you don't want them to look disabled, you can use a CSS class to modify how the disabled fields look. 如果出于某种原因,您不希望它们显示为禁用状态,则可以使用CSS类来修改禁用字段的外观。

Based on your session variable, you can embed a little piece of jQuery. 根据您的会话变量,您可以嵌入一小段jQuery。 Say $_SESSION['login'] == true , then $_SESSION['login'] == true ,然后

$(document).ready(function() {      
           $('.showbox').attr("disabled", true);           
});

and similarly for $_SESSION['login'] == false set as enabled. 同样,对于$_SESSION['login'] == false则将其设置为启用状态。

This allows you to select multiple textboxes or all of them just by adding a class or an ID to the textbox tag. 这样,您只需将一个类或ID添加到文本框标记中,即可选择多个文本框或全部选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM