Skip to content
Advertisement

How to set the input disable with condition?

I have a problem to set the input box become disable with condition. My condition is if the value is 0, then the input box will become disable. I have add readonly in the html box, but it also can’t work.

Below is what I am tried the coding, Hope anyone can guide me to check which part I am getting wrong.:

<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;">&nbsp;*</span></label>
<div class="col-lg-3">
<select class="form-control blank" id="parentid" name="parentid" title="parentid">
<option>Please Select</option>
<option value="0">New Category</option>
<?php
$sql_incharge = 'select * from filing_code_management where status=1 order by id';
$arr_incharge = db_conn_select($sql_incharge);
foreach ($arr_incharge as $rs_incharge) {
$folder_location = $rs_incharge['folder_location'];
echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
}
?>
</select> &nbsp;&nbsp;
<!--<input type="text" class="form-control blank" id="parentid" name="parentid" title="parentid" onblur="capitalize(this.id, this.value);">-->
                    </div>
                    </div>




<div class="form-group">
<label for="cp1" class="control-label col-lg-4">Activity Code:</label>
<div class="col-lg-3">
<?php 
if($rs_incharge['id'] != '0'){
 <input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code'>
                    }else($rs_incharge['id'] = '0'){
                            <input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code' readonly>
                    }
                    ?>
                        </div>
                    </div>

Actually I want the output like below the picture:

Output 1

Advertisement

Answer

Your code contains many errors.

And your question is not clear.

If you want to make input boxes as readonly when you select ‘New Category’ ( value is 0), and otherwise make input boxes editable, you need to use javascript or jQuery.

    <div class="form-group">
    <label for="cp1" class="control-label col-lg-4">Move to Sub Folder/New Category<span style="color:red;">&nbsp;*</span></label>
    <div class="col-lg-3">
        <select class="form-control blank" id="parentid" name="parentid" title="parentid">
            <option value="0">New Category</option>
<?php
    $sql_incharge = 'select * from filing_code_management where status=1 order by id';
    $arr_incharge = db_conn_select($sql_incharge);
    foreach ($arr_incharge as $rs_incharge) {
        $folder_location = $rs_incharge['folder_location'];
        echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
    }
?>
        </select>
    </div>
</div>

<div class="form-group">
    <label for="cp1" class="control-label col-lg-4">Activity Code:</label>
    <div class="col-lg-3">
        <input type='text' class='form-control' id='activity_code' name='activity_code' title='activity_code'>
   </div>
</div>

<div class="form-group">
    <label for="cp1" class="control-label col-lg-4">Activity Name:</label>
    <div class="col-lg-3">
        <input type='text' class='form-control' id='activity_name' name='activity_name' title='activity_name'>
   </div>
</div>

jQuery code

$(document).ready(function(){
    $('#parentid').change(function(){
        if ( $(this).val() == '0' ){
            $('#activity_code, #activity_name').attr('readonly', 'readonly');
        }else{
            $('#activity_code, #activity_name').removeAttr('readonly');
        }
    });
});
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement