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!!
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.