Sunday, October 18, 2009

Custom redirect after form submit

Custom redirect after form submit
Post installation · Drupal 6.x
race1 - August 2, 2008 - 15: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.
» Add new comment
drupal_goto()
mantyla - August 2, 2008 - 15:42

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.
reply
But it isn't recommended to
race1 - August 2, 2008 - 16:07

But it isn't recommended to use goto function. Other submit handlers could be skipped. Form is standart, node/add/my-content-type.
reply
Try form alter and a submit handler
Alan D. - September 29, 2008 - 18:51

The good old form alter hook still appears to be the best place for this:

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
reply
It's redirecting after
kenorb - October 22, 2008 - 06:47

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
reply
Make sure the redirect submit handler is last
Alan D. - October 22, 2008 - 15:48

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
reply
Sorry but which part of the
mortenson - November 29, 2008 - 14:39

Sorry but which part of the code should I edit or enter de url to redirect to. I am a newbie in this.
reply
Depends on what you want..
Alan D. - November 29, 2008 - 17:47

To redirect all saved nodes, you could use a code based example above and:



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
reply
Thanks, this worked for me,
pcambra - December 15, 2008 - 06:52

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
reply
No redirection at all
Dracu - March 17, 2009 - 14:08

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?
reply
Set the redirect to FALSE
Alan D. - August 21, 2009 - 01:19

Have a look at http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

Alan Davison
www.caignwebs.com.au
reply
Just a simple 'thank you' page is all..
drt417 - August 20, 2009 - 17:31

I've been looking everywhere to find out how to redirect the user to a 'thank you' page after they submit a form.

I've made a new content type, testimonial, and I want it so that anonymous uses can enter their details (client name, testimonial, email, etc), and after they submit the form, be re-directed to a thank you page instead of the node. By default, these testimonials are unpublished, so right now anonymous users are faced with 'accessed denied' after they just submitted a testimonial. (there's got to be a more friendly way).

Any insight into what I could try would be great!

Thanks!
reply
If you don't want to
Alan D. - August 21, 2009 - 01:15

If you don't want to implement the above code, try Rules, see http://drupal.org/node/298506 for details

Alan Davison
www.caignwebs.com.au

No comments: