This may be a simple question; I have a node add form, and at the end of the node add form I redirect the user to a thank you page (i.e. $form['#redirect'] = variable_get('redirect_url', ''); ) What I want to do is be able to access the contents of the node created by the form at the thank you page.

In order to accomplish this, I assume I need a way of capturing the NID of the newly created node before the redirect, and then loading the contents of that NID on the redirect page. Is my thinking correct? What's the best way to accomplish this?

Thanks!

Comments

gpk’s picture

newmediaist’s picture

Nice - I was just thinking, would a more efficient way to accomplish this be to pass the NID to the thank you page as an argument in the URL? (albeit a little more intrusive as the user would see the NID in the URL)

gpk’s picture

You could do - I mean there are no security issues really (depending on what you do with the node once you get to the thankyou page). But on the other hand $_SESSION is used for passing around huge amounts of data so a single parameter isn't going to have much impact. And it is precisely what $_SESSION is for. Also you'd have to think about what to do if someone types a made-up nid into the URL. Not an issue if you use $_SESSION.

gpk
----
www.alexoria.co.uk

elly’s picture

Hiya - I'm trying to do this exact same thing but I was trying to pass the info through the url. I'm not the greatest programmer, could anyone post a quick n dirty example of how you'd use $_SESSION to pass this info? It would be a total lifesaver. (btw, I'm doing this in Drupal 6, rather than 5)

gpk’s picture

$_SESSION is an array, best to key your values with the name of your module to guarantee no namespace collisions with stuff being stored in $_SESSION by core or other modules.

So:
To set a value:
$_SESSION['mymodule_param1'] = 'hello world'; (or could use $_SESSION['mymodule']['param1'] i.e. 2-D array)
Like any superglobal variable this is immediately available for the duration of the current page request, and at the end of the page request it is stored by Drupal in the database and is available for any subsequent page requests.
When finished, just unset it.
:-)

gpk
----
www.alexoria.co.uk

elly’s picture

thanks, that's totally helpful!

gpk’s picture

;-)

gpk
----
www.alexoria.co.uk

elly’s picture

I am SO CLOSE but I'm still missing something here. It must be something simple...

Here's what I have. Be kind, this is probably a bit of mess:


function amusa_util_form_alter(&$form, $form_state, $form_id) {
	switch ($form_id) {
        case 'model_node_form': 

	$form['buttons']['submit'] = array(
		'#type' => 'submit',
		'#value' => 'submit',
		'#weight' => '5',
		'#submit' => array('' => 'amusa_model_form_submit')
		);
	
	$form['#redirect'] = 'thanks';

	
/* get rid of a lot of unneeded options on the model create form*/
	unset($form['buttons']['preview']);
	unset($form['author']); 
	unset($form['revision_information']);
	unset($form['menu']);
	unset($form['path']);
	
return system_settings_form($form);

	}
}	

function amusa_model_form_submit(&$form, $form_state) {
	$_SESSION['amusa_model_id'] = $form['nid'];
}

So, the second function is passing the entire contents of the submitted cck form array into $_SESSION, which is KIND OF useful, but, it still doesn't have the node id of the new node in that info! Do I need to use $node_load or do I need to give that function another argument?

Thanks for any advice,
-elly

gpk’s picture

What are you trying to achieve?

BTW the usual code style for adding a submit handler would be '#submit' => array('amusa_model_form_submit'). Probably makes no difference in practice though for your case.

gpk
----
www.alexoria.co.uk

elly’s picture

Ah sorry, I posted that at like 4am and should have been clearer.

What I'm doing is: I've got a model application form node type I created with CCK, and upon the user submitting the node, I want to send them to a 'thanks' page that reports their "model id number" to them. The "model id number" will be the node id of the node they just created, but I'm having a hard time capturing the node id of the node as part of my submit function. What I've got above captures all the fields of the node form they just filled out into $_SESSION except it doesn't capture the node id!

gpk’s picture

I'm not sure if you are using Drupal 5 or 6, but in either case the node id only gets set when the node is created. Your best bet might be to use a submit handler to modify where the user gets sent immediately after node creation, and then just pull the most recently created node id for that user out of the {node} table. Another way might be to user http://api.drupal.org/api/function/hook_nodeapi/6 to set the variable in the session. $op == 'insert' would probably do it (assuming the nid has been set by then - I've not checked).

gpk
----
www.alexoria.co.uk