How to accept values ​​from a forbidden field for insertion?

I have a form that has input fields that are disabled, but nonetheless it contains values.

How can I take a variable and insert. I tried:

$value1 = $_POST['test']; 
+4
source share
3 answers

You can not. Disabled fields are not sent to your server. The Readonly field is published, so if you really need the contents of the field, you can change it so that it is read-only and not disabled.

+13
source

You can’t .

The best you can do is use default value :

 function post( $fieldName, $default = '' ) { if( !isset( $_POST[ $fieldName ] ) ) { return $default; } else { return $_POST[ $fieldName ]; } } $value1 = post('test', '0'); 

Or, as @Arjan said, change the form to readonly :

 <input type='text' name='test' readonly="readonly" /> 
+2
source

This is how the disabled attribute works. When the form control is disabled, the value will be ignored when the form is submitted, and the key will not be present in $_POST (or $_GET ).

If you want the value to be present in the presented data, but you do not want the user to be able to change the value on the page (which I suppose you are trying to achieve), use readonly="readonly" instead of disabled="disabled" .

EDIT

The <select> element does not have a readonly attribute. The above information is still preserved as it will work for <input> and <textarea> s.

The solution to your problem here is to disable the selection and use hidden input to send the value back to the server - for example,

When the selection is on:

 <select class="txtbx1" name="country"> <!-- options here --> </select> 

... and when it is disabled:

 <select class="txtbx1" name="country_disabled" disabled="disabled"> <!-- options here, with appropriate value having `selected="selected"` --> </select> <input type="hidden" name="country" value="value_of_field" /> 

But the main thing is how to set this hidden field when changing the "Select" field. and how to set when submitting the form time.

+1
source

All Articles