10

I needed to show some preexisting data from a table and but needed to disable them to prevent user from editing them. So i disabled them

$form -> getElement("elementname") -> setAttrib("disable", true);

When I submit the form, I found out, that the form element does not get submitted at all, just because it was disabled. I confirmed this when I tested removing the disable options.

What is happening? Am i doing something wrong? How to solve this?

mrN
  • 3,734
  • 15
  • 58
  • 82
  • Anyway this is not the right way to ensure that the user will no edit the field. You should add a validator to achieve this goal. – Aurelio De Rosa Oct 24 '11 at 12:27
  • 2
    If the goal is to only show the preexisting data, then do just that: print it out, do not use form fields at all. –  Jul 23 '13 at 14:54

3 Answers3

21

This is by design, disabled elements do not get submitted with the form.

What you are doing is actually a null practice, no matter what you do to that form in put it will be editable by the end user. You simply cannot trust form input - even hidden fields - to not be tampered with.

Your best bet is to just display the information to the user and load it again after the form has been submitted; at worst store it in a session.

Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
17

This worked like a charm for me. It prevents the element from being edited and will pass it through the post.

$this->username->setAttrib('readonly', 'true');
Botz3000
  • 39,020
  • 8
  • 103
  • 127
Rico Humme
  • 456
  • 5
  • 12
1

I handle these type of scenarios using hidden elements. Add a hidden element with the same content that is there in your disabled element. When the form is posted, use the value from the hidden element.

But be cautious that the use can modify the value of the hidden element using Firebug or other tools before submitting the form. Always check the form values again before processing.

Raj
  • 22,346
  • 14
  • 99
  • 142
  • 3
    `Always check the form values again before processing` If you are going to have to check the value is what you want it to be after the form is submitted, then there is no point in submitting it in the first place. – Dunhamzzz Oct 24 '11 at 15:35