Search code examples
javascriptphpshow-hide

How to hide and show the input box with the condition?


I am created the select box to choose the option, my options have condition to hide and show the input fields. My problem is how to write if else logic to check the $category_id value to show and hide in the input div in the backend page. Hope someone can guide me how to solve it. Thanks.

Below is my coding:

Frontend page:

<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'];
                            $category_id= $rs_incharge['category_id'];
                            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" id="show_hide_fc">
                        <label for="cp1" class="control-label col-lg-4">Function Code:</label>
                        <div class="col-lg-3">
                            <input type="text" class="form-control" id="function_code" name="function_code" title="function_code">
                        </div>
                    </div>
                   <div class="form-group" id="show_hide_fn">
                        <label for="cp1" class="control-label col-lg-4">Function Name:</label>
                        <div class="col-lg-3">
                            <input type="text" class="form-control" id="function_name" name="function_name" title="function_name">
                        </div>
                    </div>
                   <div class="form-group" id="show_hide_ac">
                        <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" id="show_hide_an">
                        <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>

Backend page:

<?php
$parentid = $_POST['parentid'];
$sql5 = 'select folder_location,name,category_id from filing_code_management where id='. $parentid;
$arr_sql5 = db_conn_select($sql5);
foreach ($arr_sql5 as $rs_sql5) {
$sub_category_name = $rs_sql5['name'];
$folder_location = $rs_sql5['folder_location'];
$categoryID= $rs_sql5['category_id'];
}
$show_hide_fc = $_POST['show_hide_fc'];
$show_hide_fn = $_POST['show_hide_fn'];
$show_hide_ac = $_POST['show_hide_ac'];
$show_hide_an = $_POST['show_hide_an'];
 if ($category_id == '0') {
    // $show_hide_fc will show
    // $show_hide_fn will show
    // $show_hide_ac  style display = 'none';
    // $show_hide_an  style display = 'none';

} else if ($category_id == '1') {
    // $show_hide_fc style display = 'none';
    // $show_hide_fn style display = 'none';
    // $show_hide_ac  will show
    // $show_hide_an  will show
} else if ($category_id == '2') {
    // $show_hide_fc will show
    // $show_hide_fn will show
    // $show_hide_ac  will show
    // $show_hide_an  will show
}
?>

For example if I choose the $category_id number is 1 it will show two input div, like below the sample picture.

Output 1

If I choose the $category_id number is 2 it will show 4 input div, like below the sample picture.

Output 2


Solution

  • There are two ways of solving this, depending on the size of your dataset. If there are not too many records with a $parentid (from the backend code, also please read up on SQL injection, you are inserting user data into a SQL query), you can just check the options ahead of submitting (when the form page is requested), and show or hide the items with JS depending on the option selected. This has the advantage of having no additional requests.

    If you have a lot of entries in the filing_code_managment table then you should not check them all in advance, as this would be very resource intensive, and 90% of the work done will never be seen by anyone. In this case you can use AJAX to check with the server, and check what fields will be shown or hidden depending on the result. This solution has the advantage of checking only the options that are used, but it introduces latency, as the request needs to be completed before the user can fill the next fields.

    Update with example for first option

    Example for first option in your front end code:

    <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);
                // Test array, I don't have your database
                // $arr_incharge = [
                //     ['category_id' => '0', 'id' => '0', 'name' => 'Nummer 0'],
                //     ['category_id' => '1', 'id' => '1', 'name' => 'Nummer 1'],
                //     ['category_id' => '2', 'id' => '2', 'name' => 'Nummer 2']
                // ];
                $show = [];
                foreach ($arr_incharge as $rs_incharge) {
                    $folder_location = $rs_incharge['folder_location'];
                    $category_id = $rs_incharge['category_id'];
                    echo '<option value="' . $rs_incharge['id'] . '">' . $rs_incharge['name'] . '</option>';
    
                    // Added
                    switch ($category_id) {
                        case '0':
                            $fc = true;
                            $fn = true;
                            $ac = false;
                            $an = false;
                            break;
                        case '1':
                            $fc = false;
                            $fn = false;
                            $ac = true;
                            $an = true;
                            break;
                        case '2':
                            $fc = true;
                            $fn = true;
                            $ac = true;
                            $an = true;
                            break;
                    }
    
                    // Save in one big array, to use in JS later
                    $show[$rs_incharge['id']]['show_fc'] = $fc;
                    $show[$rs_incharge['id']]['show_fn'] = $fn;
                    $show[$rs_incharge['id']]['show_ac'] = $ac;
                    $show[$rs_incharge['id']]['show_an'] = $an;
                }
                ?>
            </select>
            <!--<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" id="show_hide_fc">
        <label for="function_code" class="control-label col-lg-4">Function Code:</label>
        <div class="col-lg-3">
            <input type="text" class="form-control" id="function_code" name="function_code" title="function_code">
        </div>
    </div>
    <div class="form-group" id="show_hide_fn">
        <label for="function_name" class="control-label col-lg-4">Function Name:</label>
        <div class="col-lg-3">
            <input type="text" class="form-control" id="function_name" name="function_name" title="function_name">
        </div>
    </div>
    <div class="form-group" id="show_hide_ac">
        <label for="activity_code" 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" id="show_hide_an">
        <label for="activity_name" 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>
    <script>
        // Added, add this after the inputs
        const fc = document.getElementById('function_code');
        const fn = document.getElementById('function_name');
        const ac = document.getElementById('activity_code');
        const an = document.getElementById('activity_name');
    
        const select = document.getElementById('parentid');
        const show = JSON.parse('<?= json_encode($show) ?>');
    
        updateVisibility();
        select.addEventListener('change', function () {
            updateVisibility();
        });
    
        function updateVisibility() {
            const currentOption = show[select.options[select.selectedIndex].value];
            if (typeof currentOption !== 'undefined' && currentOption !== null) {
                if (currentOption.show_fc) {
                    fc.style.display = '';
                } else {
                    fc.style.display = 'none';
                }
    
                if (currentOption.show_fn) {
                    fn.style.display = '';
                } else {
                    fn.style.display = 'none';
                }
    
                if (currentOption.show_ac) {
                    ac.style.display = '';
                } else {
                    ac.style.display = 'none';
                }
    
                if (currentOption.show_an) {
                    an.style.display = '';
                } else {
                    an.style.display = 'none';
                }
            } else {
                // Hide everything when no known option is selected
                fc.style.display = 'none';
                fn.style.display = 'none';
                ac.style.display = 'none';
                an.style.display = 'none';
            }
        }
    </script>