Hi everyone

I'm having a bit of an issue and could do with some help. I am using the same code (for the moment) as in the Ajax Example module to make an Ajax call when a submit button is pressed. The only difference is that I'm running this form in a block not a page. However it's not making the changes I would expect. When I press the button the "Throbber" displays and then disappears but it doesn't update the form as expected. Is there something special I have to do with Ajax calls when used on a block?

Here is my code

/**
* Implements hook_block_info().
*/
function MYMODULE_block_info() {
  $blocks = array();
 
  $blocks['op_requests_block'] = array(
    'info' => t('Block displaying requests.'),
  );
 
  return $blocks;
}

/**
* Implements hook_block_view().
*/
function MYMODULE_block_view($block_name = '') {

     if ($block_name == 'op_requests_block') {
        $output = drupal_get_form('get_requests_op');
$block = array(
  'subject' => t('User Requests'),
  //'content' => $content,
          'content' => drupal_render($output),
);
   return $block;
     }
}


function get_requests_op($form, &$form_state) {
     $form['box'] = array(
        '#type' => 'markup',
        '#prefix' => '<div id="box">',
        '#suffix' => '</div>',
        '#markup' => '<h1>Initial markup for box</h1>'
    );

  $form['submit'] = array(
    '#type' => 'submit',
    '#ajax' => array(
      'callback' => 'get_requests_op_callback',
      'wrapper' => 'box',
      'name' => 'submit1',
      'effect' => 'fade'
    ),
    '#value' => t('Submit'),
   // '#id' => 'myuniqueid',
  );

  return $form;
}


function get_requests_op_callback($form, $form_state) {
$element = $form['box'];
  $element['#markup'] = "Clicked submit ({$form_state['values']['op']}): " . date('c');
  return $element;
}

So as you can see pretty much the same as the Ajax example just output on a block. I did try as can be seen commented out giving the submit a unique id but it didn't seem to help

Any ideas?

Thanks in advance for help.
James

Comments

Two issues: 1) You shouldn't

Two issues:

1) You shouldn't be making any changes to the form elements in your ajax callback. If you look in the examples, you will see they are not doing that. Doing so will cause errors in other situations (though you will probably be ok in this example), and they can be hard to debug. All changes to the form should be done in the function that defines the form. All states of the form need to be defined in the form function, and in any form alteration functions.
2) You need to render the content in the callback function before returning it.

Jaypan We build websites

Thanks for your reply

Thanks for your reply
Point 1. I would agree with you, however the ajax_examples module has this comment within the callback

// In most cases, it is recomended that you put this logic in form generation
// rather than the callback. Submit driven forms are an exception, because
// you may not want to return the form at all.

I will try as you say and put the logic in the form to see if it helps.
Point 2. rendering it before returning made no difference unfortunately.

This is the same form as is in the example module. The only difference being this is output in a block not a page. Incidentally the ajax example does work as expected.

Thanks