Examples:

Default options:


<?php echo $form->create( 'Post' ); ?>
<?php $titles = array( 1 => 'Tom', 2 => 'Dick', 3 => 'Harry' ); ?>	
<?php echo $form->input( 'title', array( 'options' => $titles ) ) ?>
<label for="nothing">This checkbox won't be send (and it won't trigger Ajax reqest)</label>
<input id="nothing" type="checkbox" name='data[Post][nothing]' value='1' />
</form>

<?php 
echo $ajax->observeField( 'PostTitle', 
    array(
        'url' => array( 'action' => 'edit' ),
        'complete' => 'alert(request.responseText)'
    ) 
); 
?>
            

Comments

  1. Edgar (Guatemala) wrote:

    I'm trying to use observeField, but I need the information of two fields of my Form, How can I use $option['with'] or/and $option['form'] or another option to obtain the information? Thank you very much, this is a very, very good helper.

    Best Regards,

    Edgar Muralles
    Guatemala

  2. kvas wrote:

    Hi Edgar,

    I think that you should observe all fields in form by observeForm or use observeField two times.

  3. Geidivan J. wrote:

    Hi,

    When I use observeForm, have any whay to know in controller wich form element has changed?

  4. kvas wrote:

    Hi Geidivan J.,

    There isn't build in parameter that tells you which element was changed, but you can put defalts in session and check which filed was changed and update new values in session.

    I think that is the best way to do your job without modifying helper.

  5. smiler wrote:

    hi kvas,

    Im using the above and specifying url to update.. is it possible to add effects to this and how?

    thanks

  6. kvas wrote:

    Hi smiler,

    What kind of effects do you want to add?

  7. Angel wrote:

    Hello kvas,

    first thanks for your great helper...

    could you please show an example of the 'position' option?

    I have tried:

    'position'=>'$("#responsediv")'
    'position'=>'$("#responsediv").html()'
    'position'=>'$("#responsediv").html(request.responseText)'

    with no success. Obviously #responsediv exists in the page...

  8. kvas wrote:

    hi Angel,
    http://www.cakephp.bee.pl/ajax/link -> using position

  9. Angel wrote:

    Thanks kvas, of course it's there.

  10. carlosb wrote:

    Hi Kvas, How can I do chained dropdown, ex if I have 3 models continents,countries, cities.

  11. kvas wrote:

    Hi carlosb,
    I think that you should do it in one request, and all data modifications should be done in one action.

  12. Thomas wrote:

    Hi Kvas,

    I'm using observeField to trigger an ajax call from a dropdown list (ProjectsCategorySelect).
    How do I send the value of the selected along with the controller call?

    Here's my code:

    echo $ajax->observeField( 'ProjectsCategorySelect',
    array(
    'url' => array('controller' => 'projects', 'action' => 'get_project_by_category', "$(\"#ProjectsCategorySelect\").val()" ),
    'complete' => 'alert(request.responseText)'
    ) );


    "$(\"#ProjectsCategorySelect\").val()" doesn't seem to work ...

    Thx for your answer!

  13. kvas wrote:

    Hi Thomas,
    You shouldn't add value in url, it's already send in POST.

    Try:
    echo $ajax->observeField( 'ProjectsCategorySelect',
    array(
    'url' => array('controller' => 'projects', 'action' => 'get_project_by_category'),
    'complete' => 'alert(request.responseText)'
    ) );

    and check $this->data in controller

  14. Jerre wrote:

    Hi kvas,

    I'm using the observefield to make something like autocomplete, except that the results wont appear as a dropdown list, but as a seperate table in a div-element.

    However I'm trying to find a way that the observerfield checks after every key pressed instead of the standard onChange event.
    Any suggestions?

    Thanks!

  15. kvas wrote:

    Hi Jerre,
    In my helper it's not possible. But you can make new function in your helper (copy of observeFiled) and replace jquery 'change' method to 'keyup'. I think that this solution will make your job.

  16. Jerre wrote:

    Thanks, that solved my issue!

  17. Bryce wrote:

    Kvas... You're awesome!

  18. Bryce wrote:

    Kvas... You're awesome!

  19. Ashish wrote:

    Hi.

    I am getting Fatal error: Call to a member function observeField() .

    After changing the $ajax to $this->Ajax, I am getting Fatal error: Call to a member function event().

    Please help.

  20. Meena wrote:

    I have an autocomplete field which shows all the cities. When i select the city, the states should be populated. I am using observeField for this. When i use keyboard to select the city, it works, but does not work when i select the city using a mouse. Please help

Post a comment

 

Additional options:

  • $options['before'] - this options will be executed before sending Ajax request
  • $options['after'] - this options will be executed after sending Ajax request
  • $options['confirm'] - this options will be displayed in confirm window
  • $options['condition'] - Ajax request will be created only if condition will return true
  • $options['position'] - where responseText should be placed (posible values: html, text, append, prepend, after, before, wrap...)
  • $options['with'] - string parameters that will passed to Ajax data option
  • $options['form'] - id of form that will be serialized adn passed to Ajax data option
  • $options['indicator'] - id of element that will be shown while browser is waiting form response from server
 

jQuery Ajax Helper

CakePHP is distributed under flexible licence - MIT Licence. I decided to publish my code on the same licence, so you can freely use IT!

If I made you interested go and download it now!