Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!