Search code examples
javascriptphponchange

Onchange show incorrect the value in the input fields


I have a problem make the onchange function to get the correct value. I want show the value in the 8 input fields if I select the option. Below is my select function coding:

<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
                  <option>Please Select</option>
                  <option value="New Category_value" 
                  data-function-code="' . $rs_incharge['function_code'] . '"
                  data-function-name="' . $rs_incharge['function_name'] . '" 
                  data-activity-code="' . $rs_incharge['activity_code'] . '" 
                  data-activity-name="' . $rs_incharge['activity_name'] . '" 
                  data-sub-activity-code="' . $rs_incharge['sub_activity_code'] . '" 
                  data-sub-activity-name="' . $rs_incharge['sub_activity_name'] . '" 
                  data-transaction-code="' . $rs_incharge['transaction_code'] . '"  
                  data-transaction-name="' . $rs_incharge['transaction_name'] . '"> 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'];
            $function_code_select = $rs_incharge['function_code'];
            $function_name_select = $rs_incharge['function_name'];
            $activity_code_select = $rs_incharge['activity_code'];
            $activity_name_select = $rs_incharge['activity_name'];
            $sub_activity_code_select = $rs_incharge['sub_activity_code'];
            $sub_activity_name_select = $rs_incharge['sub_activity_name'];
            $transaction_code_select = $rs_incharge['transaction_code'];
            $transaction_name_select = $rs_incharge['transaction_name'];
                    echo '<option value="' . $rs_incharge['function_code'] . '">' . $rs_incharge['name'] . '</option>';

                    }
                    ?>
                </select>

Jquery function:

<script>
    function getComboA() {
    var sel = document.getElementById('parentid');
    var selected = sel.options[sel.selectedIndex];
    var data = selected.getAttribute('data-function-code');
    document.getElementById("function_code").value =data;
    var data = selected.getAttribute('data-function-name');
    document.getElementById("function_name").value =data;
    var data = selected.getAttribute('data-activity-code');
    document.getElementById("activity_code").value =data;
    var data = selected.getAttribute('data-activity-name');
    document.getElementById("activity_name").value =data;
    var data = selected.getAttribute('data-sub-activity-code');
    document.getElementById("sub_activity_code").value =data;
    var data = selected.getAttribute('data-sub-activity-name');
    document.getElementById("sub_activity_name").value =data;
    var data = selected.getAttribute('data-transaction-code');
    document.getElementById("transaction_code").value =data;
    var data = selected.getAttribute('data-transaction-name');
    document.getElementById("transaction_name").value =data;
  }

</script>

Below is the error output, it cannot show the correct value, just show me the variable name when I am selected category. And if I select other selection, it also cannot show me the value:

Select Category

Output 1

Select other name no value to show out

Output 2

Whole code: https://pastebin.com/9vyvL0SG

Hope someone can guide me how to show out correct the value in the 8 input fields.


Solution

  • I cant see where your PHP code starts, try this:

    <select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid">
        <option>Please Select</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'];
                $function_code_select = $rs_incharge['function_code'];
                echo '<option value="' . $rs_incharge['function_code'] . '">' . $rs_incharge['name'] . '</option>';
            }
        ?>
    
        <option value="New Category_value" 
            data-function-code="<?php echo $rs_incharge['function_code']; ?>"
            data-function-name="<?php echo $rs_incharge['function_name']; ?>" 
            data-activity-code="<?php echo $rs_incharge['activity_code']; ?>" 
            data-activity-name="<?php echo $rs_incharge['activity_name']; ?>" 
            data-sub-activity-code="<?php echo $rs_incharge['sub_activity_code']; ?>" 
            data-sub-activity-name="<?php echo $rs_incharge['sub_activity_name']; ?>" 
            data-transaction-code="<?php echo $rs_incharge['transaction_code']; ?>"  
            data-transaction-name="<?php echo $rs_incharge['transaction_name']; ?>"> New Category
        </option>
    </select>
    

    I assume $rs_incharge['function_code'] is already defined before <select onchange...


    Example below will work cause we define $variable BEFORE we use echo:

    <?php
        $variable = 'This is awesome!';
        echo $variable;
    ?>
    

    Example below will give ERROR cause we define $variable AFTER echo:

    <?php
        echo $variable;
        $variable = 'This is awesome!';
    ?>