user1448031
user1448031

Reputation: 2166

How to use AJAX in Joomla component to load the State field based on the country selected?

Inside a component's view, I have something like this:

<?php echo TestcompHelperFind::loadStates(...); ?>
<?php echo TestcompHelperFind::loadCounties(...); ?>

The above static functions load <select> dropdowns with the state names and countries respectively.

The class TestcompHelperFind is located in the file /administrator/components/com_testcomp/helpers/find.php.

How do I load States dropdown list based on the country selected using AJAX? I'm not sure what url I should provide in the ajax function.

Upvotes: 1

Views: 1178

Answers (1)

Riccardo Zorn
Riccardo Zorn

Reputation: 5615

On the client, you will need a function that watches the country select for changes, and when it happens calls the appropriate url with a callback that will populate the counties select.

On the server, you need to output the select content.

Since you have the html output already working, let's use this approach. As an alternative you could have your server method return a json object and use the javascript to parse it and populate the select. But let's stick to html communication, i.e. the server returns the html contents of the select.

1. On the server

1.a. Output the counties select

We only need to return the result of the TestcompHelperFind::loadCounties(...); to the ajax call. This is achieved easily writing a new method in the component's controller, i.e. the controller.php in the root of the component folder or one of the sub-controllers if appropriate. It's up to you to place it in a meaningful spot.

Inside the controller simply add a new public task such as

class SomethingController extends JController
{
    public function getCountiesHTML() {
        $input = JFactory::getApplication()->input;
        $country = $input->getCMD('filter_country');
        // load helper if necessary, then:
        echo TestcompHelperFind::loadCounties($country); 
        exit; // this will stop Joomla processing, and not output template modules etc.
    }

Please note the exit; at the end, this will make Joomla output only the component's output (our echo) and not the whole template/modules etc.

1.b Add an ID to the country and county selects so that it will be possible to manipulate them on the client; I'll assume filter_country and filter_county ;

2. On the client

you will want to invoke the url

index.php?option=com_something&action=getCountiesHTML&filter_country=UK

when the country select is changed. It will also need to cancel any pending requests to avoid overlapping messages. To keep things simple, let's assume you use a library to handle Ajax, I'll write an example for jQuery:

<script>
var xhr;
jQuery(function($) {
    $('#filter_country').change(function(){
        var filterCountry = $('#filter_country').val(); 
        if (xhr && xhr.abort) {xhr.abort();xhr=false;}
        xhr = jQuery.ajax(
                url: 'index.php', 
                data: 'option=com_something&task=getCountiesHTML&filter_country='+filterCountry, 
                success: function(data){
                    jQuery('#filter_county').replaceWith(data);
                }
        );
    });
});
</script>

For cancelling the previous request, please see a dedicated answer such as this one.

Upvotes: 1

Related Questions