简体   繁体   中英

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.

Based on your session variable, you can embed a little piece of jQuery. Say $_SESSION['login'] == true , then

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

and similarly for $_SESSION['login'] == false set as enabled.

This allows you to select multiple textboxes or all of them just by adding a class or an ID to the textbox tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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