I have a form that is built in my custom module.

when setting the form I do the following things:

$form['#ajax'] = array('enabled' => TRUE,);

and for the submit button I do this:

$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#ajax' => array('submitter' => TRUE),
);

My form renders correctly on the front end and when I hit the submit button, the button changes to say "Loading..." but I can see the browser is starting to redirect. The module submit functions fire appropriatly (as my database gets written to) but then the browser redirects to the same page it was already on. only the page is just white with a textarea on it with a load of array entries being spat out.

{ "status": true, "updaters": [ ], "debug": [ ], "messages_error": [ ], "messages_status": [ ], "messages_warning": [ ], "redirect": null, "preview": null, "form_id": "custommodule_myform", "options": { "enabled": true } }

I've seen similar issues being registered for this happening with webforms, but not seen anyone saying this is happening when trying to convert forms via code.

If anyone can help that would be awesome as my deadline is approaching fast!

Thanks in advance!

Ad

Comments

adam_bush’s picture

I've managed to get this to stop happening, however I now for the life of me cannot figure out how to use code to stop this redirecting... I've placed an echo in ajax.module to check that it is running and it prints out just fine, however after the button changes to "Loading..." the page refreshes (though all of the the form submission stuff runs properly - so my flags are set and data is written to the db)

Is there anything I need to do in particular to tell a form not to redirect... i can see how to do it using ajax ui... but I need to do it for coded forms.

Many thanks

Adam

dooug’s picture

Adam,

Did you make any progress on this? I am running into similar issues with a form for the Signup module that is embedded into a view with the Views_embed_form module.

I've tried to make sense of the instructions below from the documentation. But they are somewhat unclear on what to set the #action to.

Other considerations

For anyone trying to nest a form inside of something else and have the page stay the same and the form submit correctly, do the following:

1) create a new module - or if you have a generic one you use for site-specific changes, use that. You need a .module file and a .info file
2) create a function called YOUR_MODULE_NAME_form_THE_FORM_YOU_ARE_TESTING_alter (this is an implementation of the hook_form_FORMNAME_alter) similar to this:

function node_bulk_create_form_page_node_form_alter (&$form, &$form_state) {
  if (arg(2) != 'edit') {
    $form['#action'] = url('node/add/page');
  }
}

Note that my module name above is 'node_bulk_create' and the form I'm altering is 'page_node_form'.

That's it. Note that the above only works on node creation (add) forms since that's all I'm embedding in my case. I'm not embedding the edit form. But you could. Clearly, the #action is what matters here.

FYI, I placed the call to the node form in a block, then put the block inside of a 'quicktab' displayed via 'contemplate' on another kind of content type's view page. Works great.

bsenftner’s picture

I tried setting '#action' to the url of the page, and it seems to work... no idea if that is correct.

ludo.r’s picture

I aslo get a blank page with a textarea filled with json code.

I just enabled this, in my custom module and custom form :

  $form['#ajax'] = array(
    'enabled' => TRUE,
  );
  //...
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#ajax' => array('submitter' => TRUE),
  );

I guess this is not the expected behaviour.

Am I missing something?

ludo.r’s picture

Version: 6.x-1.x-dev » 6.x-1.15