Enable and disable fields in an html form

2

My question is this:

Is it possible to enable or disable a field of a form from PHP?.

Depending on a number that I extract from a database:

  • if the number I get is 4894-rt , that I have a field in the form where I enter a datum to make a calculation.
  • but if I extract 465456-t , I do not take care that the field is enabled, but that it remains hidden.
asked by use2105 25.10.2016 в 17:26
source

4 answers

4

Ok I'll give you a simple example:

$numero = 1;
$disabled = '';
if($numero == 1){
    $disabled = 'disabled';
}
echo "<input type='text' $disabled>";
    
answered by 25.10.2016 в 17:38
1

Although the answer is "Yes" (for example, reloading the page with parameters get in the URL), I think it is 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, since the limitations of the client side can be skipped.

Greetings

    
answered by 25.10.2016 в 17:38
0

You can try something like that, check the value of $ bdresult, which is what you get in the DB, and depending on the value, show the field enabled or disabled.

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';
}
?>
    
answered by 25.10.2016 в 17:41
0
  

you can disable a field of a form and with php language enable it when you need it. ??

Yes, you just have to add the disabled attribute to the form field. For example:

<form>
  <label> Nombre: </label>
  <input type="text"<?php if ($desactivar) { ?> disabled<?php } ?> />
</form>
    
answered by 25.10.2016 в 17:45