Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will a disabled text field submit when a form is POSTed?

Tags:

If I submit a disabled text field via POST, what will the resulting value be on the action page?

For example, I have:

<table border=0 cellpadding=4 cellspacing=0> <tr><td>    <input type="checkbox" id="chk_$item"      onClick="javascript:handleClick('$item')"> </td><td>    <input type="text" id="txt_$item" name="addresses[]" value="$item"> </td></tr> <tr><td>    ...etc... </td></tr> </table> 

the handleClick() javascript function checks if chk_$item is checked, if not, it disables the txt_$item text field.

When I submit it, all of the text fields go to an addresses[] array in a PHP script.

But, can I prevent the field from submitting anything if it is disabled? Will it do this by default? If not, how should I change the behavior? (I really don't want to clear the fields when they get disabled).

like image 645
Carson Myers Avatar asked Aug 01 '09 01:08

Carson Myers


People also ask

What will happen when the disabled value is submitted?

A disabled input element is unusable and un-clickable. The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.

How do I post a disabled field?

To submit the value of a disabled input field with HTML, we replace the disabled attribute with the readonly attribute. to set the readonly attribute to readonly . This way, the user won't be able to interact with the input, but the input value will still be submitted.

What is the difference between disabled and readonly property?

The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled.

How do I disable a textbox?

We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.


2 Answers

Disabled inputs will not be submitted with the form; that's part of the defined behavior of disabled, cf. W3C HTML 4.01 Form docs.

like image 184
chaos Avatar answered Sep 29 '22 12:09

chaos


If you don't want it changed, make it readonly.

like image 41
Sampson Avatar answered Sep 29 '22 12:09

Sampson