AJAX Trigger

Last modified: September 11, 2009 - 00:27

Ajax Trigger module allows you to do specified actions on the server on JavaScript events on specified fields.

Example #1 - Calculating price in real-time using hook_ajax_trigger

1. Active Javascript Events for specified Field on Field Settings Page (admin/content/node-type/party/fields/field_name)
2. Example code:

<?php
/**
* Implementation of hook_ajax_trigger().
*/
function foo_ajax_trigger($form, $field, $op) {
 
$form_id = $form['form_id'];
  if (
$form_id == 'my_form_name') {
   
// Load required values from form
   
$price = (int)$form['field_foo'][0]['value']
   
$div = 'Calculated price: &pound;'.number_format($price,2);
   
$code = '
      if ($("#price").size() == 1) {
        $("#price").html(\'<h1>'
.$div.'</h1>\');
      } else {
        $("#edit-submit").before(\'<br><div id="price">'
. $div . '</div>\');
      }
    '
;
    return
$code;
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.