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.

Comments

mantyla’s picture

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.

demalexx’s picture

But it isn't recommended to use goto function. Other submit handlers could be skipped. Form is standart, node/add/my-content-type.

Alan D.’s picture

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

Alan Davison
kenorb’s picture

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

Alan D.’s picture

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

Alan Davison
mortenson’s picture

Sorry but which part of the code should I edit or enter de url to redirect to. I am a newbie in this.

Alan D.’s picture

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

<?php
function 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
pcambra’s picture

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

jonathan1055’s picture

Hi Alan,
Thanks very much for this. For Drupal 6 I was trying to find a hook or function I could define which would be executed after node_form_submit(). I was previously using $form['#submit'][] which executed the function before node_form_submit(). I then saw another post using $form['actions']['submit']['#submit'][] but that was not working at all for D6 (maybe it was D7 functionality).

But the code above $form['buttons']['submit']['#submit'][] adds the function after node_form_submit() which is exactly what you need in Drupal 6. For the record, I needed to use the updated $node data for the url of a custom redirect after the node is saved.

Thanks very much
Jonathan

Dracu-2’s picture

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?

Alan D.’s picture

drt417’s picture

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!

Alan D.’s picture

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


Alan Davison
Shai’s picture

In addition to the "Rules" solution suggested, the Custom Destination module is also a good alternative:

http://drupal.org/project/customdestination

Shai
Owner
Content2zero

malinica’s picture

Hi, I am trying to do something very similar so please bare with the question as I am pretty new to Drupal. I am using a custom module (webform) form to grab user information and I need to redirect it to a paypal payflow link. I need to grab all the data from the form and redirect it to a PayFlow page. I have created a custom module and am working with the appropriate hooks. I have updated both hook_form_alter and have created a handler to redirect the submit to payflow. My issue is that I need to pass in hidden input fields to payflow that I grab from the given form entries. How would I accomplish this in the handler? My code is below:

function my_module_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'my_form_id':
  	  $form['#submit'] = array('my_module_node_form_submit_handler');
   
      break;
  }
}

function my_module_node_form_submit_handler($form, &$form_state) {

 //HOW CAN I PASS IN HIDDEN VARIABLES TO THE REDIRECT PAGE HERE THAT
 //TAKE THE VALUE OF THIS FORM AND SOME ADDITIONAL PARAMTERS?
  $form_state['redirect']  = 'https://payflowlink.paypal.com';
}

Thank you for your help!

HydroZ’s picture

Watch the URL-Parameter "destination=PATH"!!!,

When your current URL contains the Parameter destination, for example:
www.homepage.org/cart?destination=shop
your setting with
$form_state['redirect'] = 'my_destination';
would be ignored, and the user will be redirected to the destination-path.
To prevent this behaviour you need to unset this with this snippet:
unset($_REQUEST['destination']);

frazras’s picture

crate a module called mymodule.info or if drupal 7 change mymodule to your themename and drop it inside the template.php file

function mymodule_form_alter(&$form, $form_state, $form_id){
	if($form_id == 'user_register') {
		$form['#submit'] []= 'mymodule_node_form_submit_handler';
	}
}

function mymodule_node_form_submit_handler($form, &$form_state) {
	unset($_REQUEST['destination']);
	unset($form['#redirect']); 

		$form_state['redirect']  = 'http://google.com'; 
	
	}
}

"I would rule the world - If only I had the source code"
R.A.Smith -- "ƒrÅzRâ§"
http://www.exterbox.com

Daniel E’s picture

In drupal 7, only this works for me (in template.php):

function seven_form_alter(&$form, &$form_state, $form_id)
{
	if($form_id == 'mytype_node_form')
	{
		$form['actions']['submit']['#submit'][] = 'seven_redirect_handler';
	}
}

function seven_redirect_handler($form, &$form_state)
{
	unset($_REQUEST['destination']); // this doesn't seem to work though
	unset($form['#redirect']); // i think this doesnt do anything because $form is not a reference

	$form_state['redirect'] = 'node/1';
}
Nikdhil Mdohfan’s picture

What if i need the node id
like

<?php
function seven_redirect_handler($form, &$form_state)
{
    unset($_REQUEST['destination']); // this doesn't seem to work though
    unset($form['#redirect']); // i think this doesnt do anything because $form is not a reference

    $form_state['redirect'] = 'node/'.$new_node_id;
}
?>
TangMonk’s picture

chrisfromredfin’s picture

Just note that using action for the form redirect means it goes to that page even if the form doesn't validate, or so it seems to me.

What really worked was altering the #submit[] array in hook_form_alter, then in that new function you register, altering form_state['redirect']

.cw.

phponwebsites’s picture

Thank you. It is worked for me.
Now i want to pass node id. But i don't like to pass node id in url like you.
You have used node id in url.

$form_state['redirect'] = 'node/'.$new_node_id;

I want to pass node id to another page without pass in url.

 $form_state['redirect'] = 'node/thankyou';

I need to get passed values on 'thankyou' page.
Is it possible? or is any other alternate ways to do?

SKAUGHT’s picture

jcmartinez’s picture

Take a look at the ThankYou module. It is very small and it gives you the flexibility of targeting different content types so you don't have to hardcode it.