POST values of disabled form elements

11,919

Solution 1

Well, there are 3 solutions I can think of:

  • Make them readonly by adding the readonly property to the element.
  • disable them in CSS/JavaScript. Color it like it's disabled, and don't allow editing with JavaScript,
  • Leave it disabled, and remove the disabled on submit.

Take your pick :)

Solution 2

you could use readonly instead of disabled wich is almost the same for the user (can't be editet) but the values of readonly-elements get submitted while the disabled ones don't.

note that there are some other differences between readonly and disabled wich might lead to other problems for you:

The Disabled attribute

  • Values for disabled form elements are not passed to the processor method. The W3C calls this a successful element.(This works similar to form check boxes that are not checked.)
  • Some browsers may override or provide default styling for disabled form elements. (Gray out or emboss text) Internet Explorer 5.5 is particularly nasty about this.
  • Disabled form elements do not receive focus.
  • Disabled form elements are skipped in tabbing navigation.

The Read Only Attribute

  • Not all form elements have a readonly attribute. Most notable, the , , and elements do not have readonly attributes (although thy both have disabled attributes)
  • Browsers provide no default overridden visual feedback that the form element is read only. (This can be a problem… see below.)
  • Form elements with the readonly attribute set will get passed to the form processor.
  • Read only form elements can receive the focus
  • Read only form elements are included in tabbed navigation.

Solution 3

There is not much point in sending the same data to and fro.

Just leave in on the server side and then use it upon submit.

Besides, assuming that

user cannot change/alter their values.

is quite silly.

Share:
11,919
bikey77
Author by

bikey77

Updated on June 05, 2022

Comments

  • bikey77
    bikey77 about 2 years

    I have a form in which I need to disable an array of checkboxes and a few fields so that the user cannot change/alter their values. When I submit the form though, the POST values of the disabled elements are missing/null. How can I manage what I'm trying to do without this problem?

    Right now I'm disabling the fields by disabling the container div like this:

    #unselectable {
        -moz-user-select: -moz-none;
        -khtml-user-select: none;
        -webkit-user-select: none;
        -o-user-select: none;
        -webkit-user-select: none;
        cursor:not-allowed; 
    
    }
    
  • bikey77
    bikey77 over 12 years
    How do I remove the disabled on submit?
  • bikey77
    bikey77 over 12 years
    Its a form in which I need to have some predefined selections according to the user type which differ from group to group, therefore the fields must be visible but not enabled.
  • bikey77
    bikey77 over 12 years
    I wrote a small script that loops through all form elements and enables them when form is submitted. Thanks!
  • Your Common Sense
    Your Common Sense over 12 years
    I am not talking of the visibility on the client side. I am talking of the server side, which already has these values - sdo, there is absolutely no point in sending them again. And about silly assumption that user cannot alter the values
  • bumperbox
    bumperbox about 10 years
    I came here looking for a solution to send disabled data to the server, when in fact I don't need to do it at all, much simpler solution. thanks
  • Chris
    Chris over 7 years
    i had the problem that a part of a form should be generated by javascript with a value that the user selects before outside the form. after the user clicked the button "add to form" the selected value should not be changed again after adding it to form but must be submitted because the serverside have to know this generated fieldname and value. therefore readonly is the perfect way!