Prevent changing checkbox value [duplicate]

心不动则不痛 提交于 2019-12-14 03:35:34

问题


As I've found no other answer over the web, I'm asking here :

I'd like to find a way to prevent people from changing the checkboxes' values.

I'm using serialize() and unserialize() to set and get checkboxes data over my database, and the problem is if someone changes the value of the checkbox (using Chrome or Firefox dev tools by exemple) it messes my retrieved values. So I'd like to know if it is possible via PHP.


回答1:


It is a bit unclear what you ask: php can only handle data on the server side. If you do not want some specific attribute of a dataset you read / write to / from a database to be modified, then just don't do it. No one forces you to consider data you get, for example inside the $_POST values you get from a form submission.

However the checkboxes themselves are only presented at the client side. Only there users can modify the values. php does not have any control over that, since it is running on the server side.

What you could do is try to prevent changes to the checkboxes by means of javascript which can be used on the client side. This works by suppressing or better swallowing the click events raised by the user. But actually there is no need for that: html itself allows to declare a checkbox as disabled which prevents any modification.

If that is not what you looking for, maybe because of optical reasons, you might want to overwrite the value of the checkboxes on html level by adding a hidden field holding the same name and value. That way it is irrelevant what modifications users make.



来源:https://stackoverflow.com/questions/30645705/prevent-changing-checkbox-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!