Front end Message add form with ajax submit

Created on 4 August 2014, over 10 years ago
Updated 2 October 2023, about 1 year ago

I'm trying to create a form in a block to enable users to add new messages. Here's my code so far, for a 'request to post' message type:

function MYMODULE_block_view($name) {  
  if ($name == 'add_request_to_post_message') {
	$content['content'] = drupal_get_form('message_ui_instance_message_manage','request_to_post');
	return $content;
  }
}

which seems to work fine, although it's not a massively elegant solution.

The bit I'm not struggling with is an ajax submission. In the above, I've tried amending the form in various ways, but with no success:

//first attempt
	$content['content']['actions']['submit']['#ajax'] = array(
        'callback' => 'MYMODULE_form_submit',
        'wrapper' => 'form-register-message'
      );

//second attempt 
        $content['content']['#submit']['#ajax'] = array(
        'callback' => 'MYMODULE_form_submit',
        'wrapper' => 'form-register-message'
      );

This is the callback:

function MYMODULE_form_submit($form, &$form_state) {
   $element = $form;
   if(!form_get_errors()) {
       $element = t('Thanks for registering. We will contact you soon');
   }
   return $element;
}

Can anyone help?

πŸ’¬ Support request
Status

Closed: works as designed

Version

1.4

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom bsarchive

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024