Support the ongoing development of Laravel.io →
Input Database Forms
Last updated 2 years ago.
0

You can achieve this with ajax request. First you can get the zones of the first country using the $countries[0]['country_id'] in your controller, so when the form is first opened, the zones of the first country will be displayed.

You need to add some jquery for an ajax call;

    $('select#country').change(function() {
        getAjax('url_of_the_ajax_controller', 'id='+$(this).val());
    });

function getAjax(url, varies)
{	
    request = $.ajax(
        {
            type: 'GET',
            url: url,
            dataType: "script",	
            data: varies		
        }
    );

    return request;
};

According to the above javascript code, the id of the country select list must be country. When you select a new country, the id of the selected country and your ajax url will be sent to the getAjax function. getAjax function will make a request to the url. So the controller of the target ajax url will get the zones of the requested country_id and will create a html select list and will change the current zones list. An example code is here:

$zones = DB::table('zones')->where('country_id', Input::get('id'))get();
        
        $html = '';
        foreach ($zones as $zone)
        {
            $html .= "<option value=\"" .  $zone['zone_id'] . "\">" .  e($zone['zone_name']) . "</option>";
        }


        return '$('#zones').html(\'' . $html. '\');';
0

i facing some problem about the same case. return '$('#zones').html('' . $html. '');'; is not working on my laravel controller.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

joelle joelle Joined 22 Jul 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.