420

I have some disabled inputs in a form and I want to send them to a server, but Chrome excludes them from the request.

Is there any workaround for this without adding a hidden field?

<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />

    <!-- this does not appear in request -->
    <input type="textbox" name="Percentage" value="100" disabled="disabled" /> 

</form>
hazzik
  • 13,019
  • 9
  • 47
  • 86
  • 4
    @Liam it is very strange attempt to close in favor to less attended question with lower quality answers. – hazzik Nov 09 '15 at 09:13
  • An **older** less attended question. Technically this question should of been closed as a duplicate when it was asked. I've flagged it with a mod for potential merging, may or may not happen. It's up to them. – Liam Nov 09 '15 at 09:53
  • @Liam in fact the questions are different even some answers looks similar. I ask about how to make "disabled" field to submit values, and another question asks about "reasons" – hazzik Nov 09 '15 at 10:11
  • Only got here after I spent a few hours trying to solve it. Ehhh.. – matcheek Nov 01 '16 at 14:21
  • Why not just set readOnly attribute to true ? – Nicolas Facciolo Apr 02 '21 at 14:44

13 Answers13

863

Elements with the disabled attribute are not submitted or you can say their values are not posted (see the second bullet point under Step 3 in the HTML 5 spec for building the form data set).

I.e.,

<input type="textbox" name="Percentage" value="100" disabled="disabled" /> 

FYI, per 17.12.1 in the HTML 4 spec:

  1. Disabled controls do not receive focus.
  2. Disabled controls are skipped in tabbing navigation.
  3. Disabled controls cannot be successfully posted.

You can use readonly attribute in your case, by doing this you will be able to post your field's data.

I.e.,

<input type="textbox" name="Percentage" value="100" readonly="readonly" />

FYI, per 17.12.2 in the HTML 4 spec:

  1. Read-only elements receive focus but cannot be modified by the user.
  2. Read-only elements are included in tabbing navigation.
  3. Read-only elements are successfully posted.
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
talha2k
  • 24,937
  • 4
  • 62
  • 81
  • 12
    To answer my own question: No, _readony_ only works for form control of type='text' and type='password' and for . What readonly does is preventing the user from changing the controls value. Preventing the user from changing value of a checkbox (or input of type radio) does not make much sense... – Muleskinner Nov 20 '14 at 16:39
  • 5
    @danielson317 You can keep the select element "disabled" but also add another hidden input with the same value. – talha2k May 05 '15 at 03:34
  • 1
    @AlphaMale But the hidden element cannot have the same name (or shouldn't). I got past this by allowing the element to be empty and just puling the original value from the saved form state. It's just worth noting readonly does not work on select form items. – danielson317 May 05 '15 at 16:17
  • Unfortunately hidden input's still sends data even if they are wrapped into disabled container... ( – Eugen Konkov Apr 09 '18 at 13:45
  • `readonly` doesn't work for checkboxes. It looks disabled, but the user can still modify it. – Nick Graham Aug 24 '18 at 18:01
  • @danielson317 It doesn't need the same name, the disabled select can have any name (as it is not submitted) the hidden field should have the true name (the one you want in the request) – Arth Oct 04 '18 at 15:35
  • Hi Googler, Lemme save you a few keystrokes. https://caniuse.com/#search=readonly – Mohd Abdul Mujib Oct 17 '18 at 13:05
  • 15
    Do you ever find something out and think, *"What in the heck made them think this would be obvious?"* If I go to the trouble of including an input field that may or may not be disabled, that means I don't want the **user** to change it, not that it should effectively act as if it was never declared at all! – Nick Bedford Apr 03 '19 at 23:09
  • 2
    Thank you this was helpful and both the answer and the response. – user1449249 May 09 '20 at 02:05
  • 3
    @NickBedford, especially when you think "Ooh, I can deal with this quickly and safely by adding the disabled attribute." – voracity Oct 12 '20 at 06:38
25

Using Jquery and sending the data with ajax, you can solve your problem:

<script>

$('#form_id').submit(function() {
    $("#input_disabled_id").prop('disabled', false);

    //Rest of code
    })
</script>
LipeTuga
  • 595
  • 5
  • 7
13

To post values from disabled inputs in addition to enabled inputs, you can simply re-enable all of the form's inputs as it is being submitted.

<form onsubmit="this.querySelectorAll('input').forEach(i => i.disabled = false)">
    <!-- Re-enable all input elements on submit so they are all posted, 
         even if currently disabled. -->

    <!-- form content with input elements -->
</form>

If you prefer jQuery:

<form onsubmit="$(this).find('input').prop('disabled', false)">
    <!-- Re-enable all input elements on submit so they are all posted, 
         even if currently disabled. -->

    <!-- form content with input elements -->
</form>

For ASP.NET MVC C# Razor, you add the submit handler like this:

using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post,
    // Re-enable all input elements on submit so they are all posted, even if currently disabled.
    new { onsubmit = "this.querySelectorAll('input').forEach(i => i.disabled = false)" } ))
{
    <!-- form content with input elements -->
}
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
9

If you absolutely have to have the field disabled and pass the data you could use a javascript to input the same data into a hidden field (or just set the hidden field too). This would allow you to have it disabled but still post the data even though you'd be posting to another page.

Chris Pierce
  • 706
  • 5
  • 9
  • 1
    This solution is what Ive been using too - but as I just learned - the readonly property is a far better solution – Muleskinner Nov 19 '14 at 22:02
6

I'm updating this answer since is very useful. Just add readonly to the input.

So the form will be:

<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />
    <input type="textbox" name="Percentage" value="100" readonly/>
</form>
Limon
  • 1,772
  • 7
  • 32
  • 61
4

Semantically this feels like the correct behaviour

I'd be asking myself "Why do I need to submit this value?"

If you have a disabled input on a form, then presumably you do not want the user changing the value directly

Any value that is displayed in a disabled input should either be

  1. output from a value on the server that produced the form, or
  2. if the form is dynamic, be calculable from the other inputs on the form

Assuming that the server processing the form is the same as the server serving it, all the information to reproduce the values of the disabled inputs should be available at processing

In fact, to preserve data integrity - even if the value of the disabled input was sent to the processing server, you should really be validating it. This validation would require the same level of information as you would need to reproduce the values anyway!

I'd almost argue that read-only inputs shouldn't be sent in the request either

Happy to be corrected, but all the use cases I can think of where read-only/disabled inputs need to be submitted are really just styling issues in disguise

Arth
  • 12,789
  • 5
  • 37
  • 69
  • 3
    not to forget that it's value can be manipulated with the chrome developer tools directly – jimjim Feb 25 '19 at 04:12
  • it's always tempting the imagine "all the use cases I can think of" == "all possible use cases". Imagine, for example, a dynamic form where an input is editable only under certain conditions, but locked for editing under others. The value of this input may change, but then be disabled do to some condition occuring. Backend still needs to updated value, even though input was eventually disabled. – powderflask Dec 10 '22 at 22:48
  • @powderflask I wasn't tempted to imagine that I'd thought of all cases, that's why I made the distinction. I guess there could be a game mechanic where you would want to stop a value increasing after certain condition occurs.. and still submit it to the backend; but you'd still need to be wary of data-integrity and validation issues as this value can still be manipulated by a semi-competent user. For the most part, however, when an input becomes disabled its because it is dependent on another form value that has changed.. and thus can be inferred from it – Arth Dec 12 '22 at 14:59
  • one should never rely on the client-side to sanitize POST data. Disabling the input widget does nothing to prevent a moderately competent user from submitting POST data for that field. Onus is always on the backend. And I do understand the general use-case, just taking issue with "all the use cases I can think of". HTTP likely works this way for historical reasons (e.g., limited bandwidth, static content), but that doesn't make it semantically correct. – powderflask Dec 13 '22 at 20:45
  • @powderflask Yes, which is exactly the point I was making, because you have to sanitise the input in the backend anyway - and it's likely inferable from whatever conditions disabled the input - you likely don't need it submitted. Not expecting/reading the disabled input will protect you from malicious users submitting erroneous data for it. OK, well if you have a specific use-case which requires you to submit disabled inputs, then I'd suggest using one of the other answers. This answer is more of an 'Are you asking the right question?' – Arth Dec 14 '22 at 09:45
3

I find this works easier. readonly the input field, then style it so the end user knows it's read only. inputs placed here (from AJAX for example) can still submit, without extra code.

<input readonly style="color: Grey; opacity: 1; ">
2

Simple workaround - just use hidden field as placeholder for select, checkbox and radio.

From this code to -

<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />

    <!-- this does not appear in request -->
    <input type="textbox" name="Percentage" value="100" disabled="disabled" /> 
    <select name="gender" disabled="disabled">
          <option value="male">Male</option>
          <option value="female" selected>Female</option>
    </select>

</form>

that code -

<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />

    <input type="textbox" value="100" readonly /> 

    <input type="hidden" name="gender" value="female" />
    <select name="gender" disabled="disabled">
          <option value="male">Male</option>
          <option value="female" selected>Female</option>
    </select>
</form>
Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38
1

use

<input type="textbox" name="" value="100" disabled/>

or

<input type="textbox" name="" value="100" readonly/>

if your are using framework like PHP Laravel, element without name attribute will read as unset

<input type="textbox" value="100" disabled/>
0

In addition to Tom Blodget's response, you may simply add @HtmlBeginForm as the form action, like this:

 <form id="form" method="post" action="@Html.BeginForm("action", "controller", FormMethod.Post, new { onsubmit = "this.querySelectorAll('input').forEach(i => i.disabled = false)" })"
Pedro Coelho
  • 1,411
  • 3
  • 18
  • 31
  • 1
    Have you tried this code? Because I don't see how it would work. `@Html.BeginForm(...) {...}` renders `
    ...
    `, which would not be a great thing to have in an attribute. Perhaps you meant `@Url.Action("action", "controller")`, which renders a URL?
    – Heretic Monkey Jun 26 '20 at 18:42
  • I tried this code, Heretic. It worked here. Before, I was using as you mentioned, with @Url.Action, but there were not all the params. Perhaps the "using" is better practive, but I'm not sure – Pedro Coelho Jun 26 '20 at 20:14
0

Define Colors With RGBA Values

Add the Following code under style

<!DOCTYPE html>
<html>
<head>
<style>
#p7 {background-color:rgba(215,215,215,1);}
</style>
</head>
<body>
Disabled Grey none tranparent
<form action="/Media/Add">
    <input type="hidden" name="Id" value="123" />

    <!-- this does not appear in request -->
    <input id="p7" type="textbox" name="Percentage" value="100" readonly="readonly"" /> 

</form>

result

0

I had exactly the same problem, but did not work for me, because I have select HTML element, and it's read-only state allowed to change its value. So I used select in one condition and input in another:

   <% If IsEditWizard Then   %>
                            <%For Each item In GetCompaniesByCompanyType("ResponsibleEntity")%>
                            <% If item.CompanyCode.EqualsIgnoreCase(prCompany.GetAsString("LinkedCompany")) Then   %>
                            <input type="text" value="<%: item.CompanyName  %>" tabindex="3" size="12" maxlength="12" readonly="readonly" />
                            <input type="hidden" id="LinkedCompany" name="LinkedCompany" value="<%:item.CompanyCode %>" tabindex="3" size="12" maxlength="12" />
                            <%End If %>
                            <%Next %>
                            <%Else %>
                            <select id="LinkedCompany" name="LinkedCompany" class="w-auto" <%= If(IsEditWizard, "disabled", "") %>>
                                <option value="">Please Select</option>
                                <%For Each item In GetCompaniesByCompanyType("ResponsibleEntity")%>
                                <option value="<%:item.CompanyCode %>" <%: IIf(item.CompanyCode.EqualsIgnoreCase(prCompany.GetAsString("LinkedCompany")), "selected", String.Empty) %>><%: item.CompanyName %></option>
                                <%Next %>
                            </select>

                            <%End If %>
Mr.B
  • 3,484
  • 2
  • 26
  • 40
-7

You can totally avoid disabling, it is painful since html form format won't send anything related to <p> or some other label.

So you can instead put regular

<input text tag just before you have `/>

add this readonly="readonly"

It wouldn't disable your text but wouldn't change by user so it work like <p> and will send value through form. Just remove border if you would like to make it more like <p> tag

Neels
  • 2,547
  • 6
  • 33
  • 40