Populate 2nd drop-down list based on 1st drop-down using API JSON

纵然是瞬间 提交于 2020-01-25 06:59:07

问题


I want to display data at 2nd drop-down list base on data chosen from 1st drop-down list. I used AJAX to get display data at 2nd drop downlist.

Below is the JSON result if facID = F09

Below is the AJAX code at where the drop-down list is located

<script>
    function getroom(val) {
      $.ajax({
        type: "POST",
        url: "../room_scheduler/room_scheduler.php",
        data:'factory_id='+val,
        success: function(data){
          $("#room-list").html(data);
        }
      });
    }
</script>   

and below is the room_scheduler.php

    <?php

    require_once "../../../config/configPDO.php";
    require_once "../../../config/check.php";

    //retrieve json
    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/roomList?facID='" . $_POST['factory_id'] . "'";
    $data = file_get_contents($url);
    $characters = json_decode($data);

        if(!empty($_POST["factory_id"])) {

            echo '<option value="">Select</option>';
            foreach ($characters->roomList as $character) {
            echo "<option value='$character->roomId'>$character->Room_Desc</option>";
            }

        }

    ?>

The result if, if in the 1st drop-down list I choose F09, there's no data display at the 2nd drop-down list. Can I know what is the problem?


回答1:


Sorry guys, this question already solved by myself

    <?php

    require_once "../../../config/configPDO.php";
    require_once "../../../config/check.php";

    $factory_id = $_POST["factory_id"];

    //retrieve json
    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/roomList?facID=$factory_id";
    $data = file_get_contents($url);
    $characters = json_decode($data);

        if(!empty($factory_id)) {

            echo '<option value="">Select</option>';
            foreach ($characters->roomList as $character) {
            echo "<option value='$character->roomId'>$character->roomDesc</option>";
            }

        }


    ?>


来源:https://stackoverflow.com/questions/59819513/populate-2nd-drop-down-list-based-on-1st-drop-down-using-api-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!