views:

51

answers:

1

I'm quite newby with ajax in cakephp and what I want to do is update one select form input element when user changes other.

I have managed to do it previously with JQuery but I can't use it on this form, because it seems to conflict with prototype library that is used by $ajax->autoComplete() on one form element.

I couldn't get it to work with remoteFunction and observeField. Probably because I don't know how to use them right.

Can someone tell me or point out some good material, how to achieve this the cakephp way? Or how you have managed to make it work?

A: 

Alright, learned to use observeField with $options['complete'] and made a function that replaces other select input content with data from JSON. Lot of learning done today :)

...
echo $this->Form->end(__('Submit', true));
echo $this->Ajax->observeField('ReviewTyreMakeId', array(
    'url' => array('controller' => 'tyre_models', 'action' => 'getTyreModels'),
    'complete' => 'updateTyreModels(request);',
    ))
...


function updateTyreModels(response){
    var data = response.responseText.evalJSON(true);
    var options = "";
    var propCount = 0;
    for (x in data) {
        options += "<option value=\"" + x + "\">" + data[x] + "</option>";
        propCount++;
    }
    if (propCount <= 1)
        $('ReviewTyreModelId').disable();
    else
        $('ReviewTyreModelId').enable();
    $('ReviewTyreModelId').innerHTML = options;
}

Feel free to comment the code

kaupov