Ajax/cakephp 2. Click into one checkbox, and populate two different select boxes

60 views Asked by At

I´m trying to click into one checkbox and populate two diferent select boxes.

I have to populate the brand select box and the model select box.

If the given car is the same car from the last visit for a given client person, returning into an car repair shop.

My starting point is the jquery ajax below.

But it is created to populate just one select box.

I trying to figure out how to populate the two diferent select boxes, after clicking into just one checkbox.

checkbox id: sameCarFromLastVisit brand select box id : brandId model select box id : modelId

For the backend i am using cakephp 2.

How can it be done ?

$(function(){

    $(document).on('click', "#sameCarFromLastVisit", function()
    {
        var url = '<?php echo Router::url(array('controller' => 'Cars', 'action' => 'getBrandModel_ajax')); ?>';
        var checked = $(this).is(':checked');
        var data = {
            checked: $(this).val()
        };

        $.ajax({

            url: url,
            method: 'POST',
            data: data,
            dataType: 'json',

            beforeSend: function( xhr ) 
            {
                if(checked == 1){

                    $.blockUI({
                        message: '<h2>Please wait! searching brand and Model</h2>'
                    });

                }

            },
            complete: function () 
            {
                $.unblockUI();
            },
            success: function ($response, a, b) 
            {
                if(b.status == 200) 
                {
                    var $optionsBrand = $("#brandId").html($("<option />").val('').text('- selecione -'));

                    if($response)
                    {
                        console.log('response '+$response);
                        $optionsBrand.prop("disabled", false);

                        $.each($response, function(idbrand, value)
                        {
                            var $opt = new Option(value, idbrand);

                            $optionsBrand.append($opt);
                        });
                    }
                    else
                    {
                        $optionsBrand.prop("disabled", true);
                    }

                }
                else 
                {
                    alert($response);
                }
            },
            error: function(x, t, m) 
            {
                var msg = 'An unexpected error occur. ';

                if( t === "timeout" )
                {
                    msg += "wait 30 segundos and try again!";
                }
                else
                {
                    msg = t;
                }

                alert( msg );
            }
        }); //ajax
    }); // document on 'click'

}); // function
0

There are 0 answers