1

Edit: I did some more research I think I got closer but yet it doesn't work. I changed some of the codes.

Hello It's my first question. I searched a lot but I couldn't find my answer. Ok I've got two simple dropdowns which'll be filled with data from a certain database table. the table is called 'mobiles' and the columns are like this :

| id | brand | model | price

it's a simple onepage website. so when the home page controller looks like this:

public function homePage()
{
    $brands = Mobile::all('brand')->lists('brand');
    return view('pages.home')->with('brands', $brands);
}

I've just populated the mobiles table with 2 phones right now. an Apple iPhone 6 and a Samsung Galaxy S6. And the form looks like this :

            <div class="form-group">
            {!! Form::label('brand','Brand') !!}
            {!! Form::select('brand', $brands, null, ['class' => 'form-control'])  !!}
        </div>
        <div class="form-group">
            {!! Form::label('model','Model') !!}
            {!! Form::select('model', $models, null, ['class' => 'form-control']) !!}
        </div>

First 'the not so important problem' is that I can't find a way to select distinct brand values from my table but that's not the problem right now. the real problem is I don't know the where the problem of my Ajax script is. The Edited Script now is :

    <script>

    $("document").ready(function(){
        $('#brand').change(function(){
            $.getJSON("{{ url('api/getmobiles')}}",
                    { option: $('#brand option:selected').text() },
                    function(data) {

                        var model = $('#model');
                        model.empty();

                        $.each(data, function(i, value) {
                            $('#mobile').append($('<option>').data.model.attr('value', data.id));


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

and the new route is :

Route::get('api/getmobiles', function() {
$input = Input::get('option');
$models = DB::table('mobiles')->where('brand',$input)->lists('model','id');
return Response::json($models);
});

Please be gentle. I'm new to Laravel. I just found everything on the internet but there seems to be lack of decent tutorial on using Ajax in Laravel.

Armando
  • 11
  • 2
  • Your data contain the response, which is a json array, with the mobiles. You have to populate your mobiles select, this will go to your success function, something like this: http://stackoverflow.com/questions/815103/jquery-best-practice-to-populate-drop-down – Iamzozo Oct 26 '15 at 07:31
  • @lamzozo Thank you for your answer and the link you provided. so everyhing else seems fine? – Armando Oct 26 '15 at 07:34
  • `data : $("#brand").text(),` should be `data : $("#brand").val(),` – Abdulla Nilam Oct 26 '15 at 07:35
  • @Abdulla Thanks for your answer, well I haven't indexed my brand column, so I wanted it to select the text of it. val() returns a number I think right? – Armando Oct 26 '15 at 07:38
  • `val()` returns value of the field – Abdulla Nilam Oct 26 '15 at 07:43

0 Answers0