Enable and disable fields in an html form
My doubt is as follows:
Is it possible to enable or disable a field of a form from PHP?.
Depending on a number I extract from a database:
- if the number I extract is
4894-rt
, let me enable a field in the form where I will enter a data pata that makes a calculation. - but if I extract
465456-t
, I do not occupy to have the field enabled, but to keep it hidden.
4 answers
Ok I'll give you a simple example:
$numero = 1;
$disabled = '';
if($numero == 1){
$disabled = 'disabled';
}
echo "<input type='text' $disabled>";
Although the answer is " yes " (e.g. reloading the page with get parameters in the URL), I think it's much better to use JavaScript or jQuery.
In JavaScript for example to disable:
document.getElementById("idInputEnElDom").disabled = true; // deshabilitar
document.getElementById("idInputEnElDom").disabled = false; // habilitar
In Jquery
$("#idInputEnElDom").prop('disabled', true); // deshabilitar
$("#idInputEnElDom").prop('disabled', false); // deshabilitar
$(".claseInputEnElDom").prop('disabled', true); // deshabilitar por clase, posibilita deshabilitar multiples campos con una instrucción
Eye, on the server side you will have to control the case, as the client side limitations can be skipped.
Greetings
You can try something like this, which checks the value of $bdresult which is what you get in the database and depending on the value you show The enabled or disabled field.
Example:
<?php
if ($bdresult === "4894-rt") {
echo '<input type="text" value="abc">';
} elseif ($bdresult === "465456-t") {
echo '<input type="text" value="abc" disabled>';
} else {
echo 'otra cosa';
}
?>
You can disable a field of a form and with php language enable it when you need it.??
Yes, just add the disabled attribute to the form field. For example:
<form>
<label> Nombre: </label>
<input type="text"<?php if ($desactivar) { ?> disabled<?php } ?> />
</form>