Custon redirect after form submit
race1 - August 2, 2008 - 22:23
Hello! I searched how to redirect user after form submit but unfortunately it isn't helpful for me. I have my own content type and nodes of that type printed manually, using menu system - user goes to /drupal/show_my_content_type_page/$id and callback function displays data.
When user add node of my content type I'd like to redirect him to /drupal/show_my_content_type_page/$id. I know how to redirect user to /drupal/show_my_content_page/ using #redirection, but I need to redirect him to /drupal/show_my_content_page/$id so user see created page.
How could I do this? Thanks.

drupal_goto()
I'm not sure if you want to redirect users after submitting a contrib form or core form, so here's what helps with both: if you have a contrib function, just add a drupal_goto() call with the parameters you want in the end of the submit function. If it is a core form, create a custom submit function which calls drupal_goto() and attach it to the forms you want using a hook_form_alter() implementation.
But it isn't recommended to
But it isn't recommended to use goto function. Other submit handlers could be skipped. Form is standart, node/add/my-content-type.
Try form alter and a submit handler
The good old form alter hook still appears to be the best place for this:
<?php
/**
* Implementation of hook_form_alter().
*/
function jm_form_alter(&$form, $form_state, $form_id) {
if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
$form['buttons']['submit']['#submit'][] = 'jm_redirect_handler';
}
}
/**
* Attaches the redirect to the submitted form.
*
* @param unknown_type $form
* @param unknown_type $form_state
* @return unknown
*/
function jm_redirect_handler($form, &$form_state) {
if ($form_state['nid']) {
// reloading as I do not know the node type context. You probably do not need to :), just set the redirect using $form_state['nid']
$node = node_load(array('nid' => $form_state['nid']));
switch($node->type) {
case 'project':
$form_state['redirect'] = 'projects/'. $node->nid;
}
}
}
?>
This needs to be wrapped in a module, but works nicely. I'm ignoring delete as the standard works fine.
Alan Davison
www.caignwebs.com.au
It's redirecting after
It's redirecting after submitting the form, what about permissions?
node_save function run firstly nodeapi (submit hook) and after that acquire_grants hook, if you do redirection during submit, you will lost some permissions (acquire_grants hook). Am I right?
Tested on Drupal 6.x
Make sure the redirect submit handler is last
If you are using the above code, make sure that your custom submit handler is last. It doesn't stop the other handlers from working, it just adds a redirect that overwrites the normal redirect.
The form redirect is the last step in the form workflow, after the submit handlers. On some forms you can set the #redirect property during form creation, but most forms do this during submission. It is only a variable to flag what do do after you have finished.
Alan Davison
www.caignwebs.com.au
Sorry but which part of the
Sorry but which part of the code should I edit or enter de url to redirect to. I am a newbie in this.
Depends on what you want..
To redirect all saved nodes, you could use a code based example above and:
<?phpfunction jm_redirect_handler($form, &$form_state) {
if ($form_state['nid']) {
// redirecting all nodes
$form_state['redirect'] = 'my-thank-you-page';
}
}
?>
It think that if your unsure about the proggramming, then an action or the rules module could be an easier method of doing redirects. No programming would be needed.
Using rules, define a new triggered action, via admin > rules, and define the condition that checks on the node that is created, then add an action "page redirect". I don't think you can get info about the saved node here though, so if this doesn't matter use rules, and if you require the nid, etc, use the code based one.
Or place a really nice request to the rules issue queue and wait patiently for a fix :)
Cheers
Alan Davison
www.caignwebs.com.au
Thanks, this worked for me,
Thanks, this worked for me, I was trying with $form['#submit'][] but it does not work, when i changed that to the $form['buttons']['submit']['#submit'][] you suggested, all went straight
No redirection at all
I have a Html Form, completely hidden. I collect data from that Form in my online database by using java autosubmit.
So visitors don't have to click on something, and they also they don't have to see the hidden form.
My problem is with the stupid redirection. How can I get rid of it. I dont need no redirection at all, not even refresh or reload the page.
I need my visitors to stay on the original page they load without seeing anything!
Can someone give me an idea how can i work it out?