function ahah_simple_menu($may_cache) {
  $items = array();
  if (!$may_cache) {
  $items[] = array(
    'path' => 'ahah/simple',
    'title' => t('Ahah Simple'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array('ahah_simple_page'),
    'access' => TRUE,
  );
  $items[] = array(
    'path' => 'ahah/simple_js',
    'title' => t('Never seen'),
    'callback' => 'ahah_simple_js',
    'type' => MENU_CALLBACK,
    'access' => TRUE,
    );  
  }
  return $items;
}

function ahah_simple_page() {
  $form = array();
  $form['target'] = array(
    '#type' => 'item',
    '#value' => "Don't do it!",
    '#prefix' => '<div id="target">',
    '#suffix' => '</div>',
  ); 
  $form['submit'] = array(
    '#type' => 'button',
    '#value' => t('Click Me'),
    '#ahah_bindings' => array (
      array( 
      'event' => 'click',
        'path' => 'ahah/simple_js',
        'wrapper' => 'target'
      )) ,
  );
  return $form; 
}

function ahah_simple_js() {
  print '<p>No one ever listens.</p>';
}

I've made some very minor changes as you can see which should not affect the functionality of this module, but which make it slightly easier to use (having a title for the path means that it shows up in the menu for instance).

My first suspicion was that the jQuery version you were using was different than mine, so I updated to v1.6 (of the Drupal version of jQuery).

My second suspicion was that I needed to adjust the paths, so I tried using url('ahah/simple_js'), but this doesn't seem to work either.

Once I can get the simple version to work, then I can use this module to extend one of my modules, which I am very excited about.

Thank you,

Dave

Comments

dwees’s picture

Forgot URL to problem site. It is: http://www.unitorganizer.com/drupal5/ahah/simple

Thanks again.

David Wees

starbow’s picture

Title: Ahah forms framework in a sub-folder » Example Simple not working
Priority: Normal » Minor

I think you might have come across a small bug in the simple example. Since submit buttons do not automatically get an id, the binding tries to use the submit class, which only works if it is the only submit button on the page. Try this change

$form['submit'] = array(
'#type' => 'button',
'#value' => t('Click Me'),
'#attributes' => array( 'id' => 'simple_submit' ),
'#ahah_bindings' => array (
array(
'event' => 'click',
'path' => 'ahah/simple_js',
'wrapper' => 'target'
)) ,
);

I don't know if that will work, but if it does, let me know and I will roll it into the module.

dwees’s picture

Status: Active » Fixed

Found a fun fact about the forms API which makes the ID attribute very interesting. It has an #id attribute which and you can set an 'id' attribute, and the '#id' attribute of the form is set to 'edit-submit' for a submit button, and I set the 'id' attribute to 'simple_submit' as suggested. As a result the HTML id of the submit button is 'simple_submit' but the internal id which your module reads is 'edit-submit' so after your change, the example still wasn't working.

I can't just set the '#id' attribute because this is used internally only, so I have to set both. Code example below, and this works on my site now.


function ahah_simple_page() {
  $form = array();
  $form['target'] = array(
    '#type' => 'item',
    '#value' => "Don't do it!",
    '#prefix' => '<div id="target">',
    '#suffix' => '</div>',
  ); 
  $form['submit'] = array(
    '#type' => 'button',
    '#value' => t('Click Me'),
    '#attributes' => array( 'id' => 'simple_submit' ),
    '#id' => 'simple_submit',
    '#ahah_bindings' => array (
      array(
        'event' => 'click',
        'path' => 'ahah/simple_js',
        'wrapper' => 'target'
    )),
  );
  return $form; 
}

I'm not sure if I'm supposed to be able to set the internal '#id' element of the form, but whatever it works.

starbow’s picture

Yeah, the proper use of #id is a little unclear. I just tested it with just the #id, no #attributes, and that seems to work fine, so I will update ahah_simple.module.

cheers,
-tao

Anonymous’s picture

Status: Fixed » Closed (fixed)