How to manage dependent dropdown list in php when create and update method is managed in same page

  ajax, html-select, javascript, mysql, php

I have written the code to display the dependent dropdown list in php, when I create the record it is working. What changes I need to do while updating the record as I want the create and update method in same page.
Here’s the code for reference,

//html
    <div class="form-group row m-b-15">
    <label class="col-md-4 col-sm-4 col-form-label" for="fin_is_fin_req">
        Is Finance Required? * :
    </label>
    <div class="col-md-4 col-sm-4">
        <select name="fin_is_fin_req" id="is_finance" class="form-control" autofocus>
            <option value="">-Select Finance-</option>
            <?php
            $fin_req=array('Yes','No');
            foreach ($fin_req as $list) {
                if ($allotment->fin_is_fin_req==$list):?>
                    <option selected value="<?=$list?>">
                <?php else:?>
                    </option> <option value="<?=$list?>">
                <?php endif;?>
                <?=$list?></option>
            <?php } ?
        </select>
    </div>
</div>

<div class="form-group row m-b-15 ">
    <label class="col-md-4 col-sm-4 col-form-label" for="fin_stage">
        Finance Stage * :
    </label>
    <div class="col-md-4 col-sm-4">
        <select name="fin_stage" id="stages" class="form-control" >
            
        </select>
    </div>
</div>


//js
$(document).ready(function(){
 $('#is_finance').on('change', function () {
        var finance_value=$("#is_finance").val();
        $.ajax({
            url:'ajax/loadstages.php',
            method:'post',
            data: 'finance_value='+finance_value
        }).done(function (stages) {
       stages=JSON.parse(stages);
            stages.forEach(function(stage){
                $('#stages').append('<option value='+stage.id+'>'+stage.stage_name+'</option>')
            })
        })

    });
});


//loadstages.php


if (isset($_POST['finance_value'])){
    if($_POST['finance_value']=="Yes"){
        $stages=FinStage::select('*','state=1');
        //select(rows,where)
    }else{
        $stages=FinStage::select('*','state=0');
    }
}
echo json_encode($stages);

//screenshot during insert
screenshot during insert

//screenshot during update
screenshot during update

Source: Ask Javascript Questions

LEAVE A COMMENT