Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Input field read-only when it has value from database otherwise when it's null it should be editable

I am new to this community, Your help is highly appreciated. This is my line of code:

<input type="text" class="form-control" id="fines" name="fines" placeholder = "Amount Penalty" required>

My Javascript code:


$(document).ready (function(){



  if($(this).val()==""){
    $('#fines').attr('readOnly','false');
  }
  

});

</script>```




The thing that I want to achieve is that when clicking the field with no value it must be editable and if it has a value from the database it will remain a read-only field. 

Your help is highly appreciated thank you!!



  
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can do something like this, as you are using a php variable for text input.

Method 1:-

<?php
    if( $row['fines'] == '' || $row['fines'] == NULL){
  echo '<input type="text" class="form-control" value="" id="fines" name="fines" placeholder = "Amount Penalty" required>';
    }
    else{
  echo '<input type="text" class="form-control" value="'.$row['fines'].'" id="fines" name="fines" placeholder = "Amount Penalty" readonly>';
    }
?>

Method 2:-

<?php
    if( $row['fines'] == '' || $row['fines'] == NULL){
      echo '<input type="text" class="form-control" value="" id="fines" name="fines" placeholder = "Amount Penalty" required>';
    }
    else{
      echo $row['fines'];//Show it as plain text, ez
    }
?>

To Note :- When you use Method 1:- there are still chances of making that input text writable, just inspecting it and changing value would be easy enough for a user, I think Method 2:- is much better.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda