Prak
Prak

Reputation: 825

How to call Ajax on drop down in CakePHP

I am new to PHP and CakePHP. I m trying to call Ajax on changing drop down item. I have done it for a link as below code

echo $this->Html->link('TestLink', array('controller'=>'Tutors','action'=>'getData',$iid), array('class'=>'js-ajax')); 

but unable to apply on drop down. I have used the code for drop down as below

echo $this->Form->input('My City', array('empty'=>'Select City','options' => $cities));

here, where to put code to call ajax. Please help.

Upvotes: 0

Views: 771

Answers (3)

Colonel Mustard
Colonel Mustard

Reputation: 1533

If its just the selector you're looking to add to trigger the ajax, then do the following:

echo $this->Form->input('My City', array(
                                 'empty'=>'Select City',
                                 'options' => $cities,
                                 'class' => 'js-ajax'
                        ));

Then you can have your javascript onchange called with this selector

Upvotes: 1

ErasmoOliveira
ErasmoOliveira

Reputation: 1426

Why you don't use jquery to do this?

put in your webroot and call in your layout(app/view/layouts)

$(document).ready(function(){

    //your code
    $("#youFieldId").change(function(){
        //Your logic
    });


});

Upvotes: 0

Alex M.
Alex M.

Reputation: 641

You should bind an ajax event to js-ajax

$('.js-ajax').on('change', function(){
  // do some ajax here.
});

`

Upvotes: 1

Related Questions