samedi 1 juin 2019

Laravel: dynamic drop down using jax not returning data

I'm trying to use ajax dynamic dropdown list from the database. so here is the Scenario i want:

-If i select a schedule in the dropdown select i want all related buses to that schedule to populate on that lower bus dropdown.

enter image description here

so far i was having everthing right! i mean i tested that data canbe passed to the controller, also Controller returned the json of the data. But when i tried to populate the select Bus options to the html in the success funcitoin nothing come up up to the bus dropdown.

I don't know what i missed, but sure there's an issue

View

[![enter image description here][2]][2]

here's theScript

<script type="text/javascript">
$(document).ready(function(){

    $(document).on('change','.schedule',function(){
        // console.log("hmm its change");

        var id=$(this).val();
        // console.log(id);
        var div=$(this).parent();

        var op=" ";
  // console.log(div);


        $.ajax({
            type:'get',
            url:"",
            data:{'id':id},
            success:function(data){
                //console.log('success');

                //console.log(data);

                op+='<option value="0" selected disabled>chose Bus Number</option>';
                for(var i=0;i<data.length;i++){
                  op+='<option value="'+data[i].id+'">'+data[i].bus_number+'</option>';
              }

              div.find('#busnumber').html(" ");
      // console.log(div);
              div.find('#busnumber').append(op);
      // console.log(div);



            },
            error:function(){

            }
        });
    });
});
</script>

Controller

public function getBusStation(Request $request)
{ 
    $id = $request->id;
    $queue = Schedule::find($id)->queue;

    $data = $queue;  
    return Response()->json($data);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire