How can I disable an input field without affecting the field value in a PHP form?

I am creating a demo page where some settings fields need to be disabled. I tried to disable the input with the remaining unused value, just greyed out. I disabled the input with disabled = "true". When the form is submitted, the value disappears, even though it was before. How can I prevent the value from disappearing while disabling the specified fields?


source to share

5 answers


form fields are not submitted under any circumstances.

The most common way to avoid this problem is to make them readonly

and add a input[readonly] { ... }

css rule to make the text gray like in a disabled box.

You can also use JavaScript to prevent it from focusing; might look like this if you have jQuery:

$('input[readonly]').live('focus', function(e) {




If you want the value to be displayed and not changed, you can use readonly

<input type="text" name="xxx" value="xxx" readonly="readonly" />


If you want the value to be hidden and sent to the action file you can use type = hidden

<input type="hidden" name="xxxx" value="xxx" /> 


More details on the HTML input tag can be found here



use readonly attribute instead of disable

another approach would be to store the values ​​in the PHP session and store them on the server.



You can also use a hidden field with the same name with the correct value and keep the field disabled. Though don't 100% trust the data in the hidden / read field as it can still be changed (via web developer tools or similar)



Just drop offers here.
You can disable this field as usual with Javascript, but before disabling the field, you can use Javascript to enter the value of this field into the hidden field.

function runme() {
    var newval = $('#field1').val();
    $('#field1').attr('disabled', 'disabled');

<input id="field1" name="field1" type="text" value="this val" />
<input id="hiddenfield" name="hiddenfield" type="hidden" />
<input type="button" onclick="runme();" />




All Articles