Hi,
A form collects input from the user e.g. budget, interest etc.

Then its submit

function custom_submit($form, &$form_state) {

uses the submitted values to retrieve the values from the database table. Now how do I display the collected info? In general PHP application the page displays the info (to which form is submitted using ACTION).

But using drupal API we simply write validate and submit handlers. So here I'm stuck :-)
Please guide,
Regards

Comments

graysadler’s picture

The submit handler simply performs a procedure, usually saving data captured from the form input. After the form submits, the form/page is either reloaded or the submit handler redirects to another url. You can display the saved data on that same form if you'd like or display it on another page that the user is redirected to.

Either option you choose requires a query the db for the values you just saved and return the data in html format.

Sorry my reply is so vague. If you were more specific in your question you would get a more specific answer. =)

Lead Developer and Founder of StreamRiot.com

I am learning’s picture

exactly,

or the submit handler redirects to another url.

That's what my confusion is and so far I've been doing the same, but the problem is that I've to send either retrieved information or the form values (that I got as POST) as query string parameter.

function custom_module_form_submit($form, &$form_state) {
  $path = "view-details/".$form_state['values']['budget'];
  $options['alias'] = TRUE;

  $form_state['redirect'] = $path;

is it the correct approach? I feel a little uneasy with this, I feel I'm doing it in a wrong manner.

jason_gates’s picture

Hi,
Please take a look at the source listing includes/form.inc .

  1. Perform a text search for $form_state['redirect']. You'll find that "$form_state['redirect']" is used as a parameter in a function call.
  2. Write down the name of the function that uses "$form_state['redirect']" as a function call parameter (e.g. result of step 1). Lets call the function you just wrote down FUNCTION A
  3. Now find the method definition and source for FUNCTION A (hint -- it's also located in ncludes/form.inc ). Do a text search on FUNCTION A (i.e inside the file listing ncludes/form.inc ).
  4. Inspect the source code for FUNCTION A and see which API method is using. Lets call the API method METHOD B
  5. Look at the API documentation here http://api.drupal.org/api/drupal and see how you use METHOD B to send parameters

Good luck. Remember it's your responsibility to research an issue before submitting to this forum ( see http://drupal.org/forum-posting ).

Also please rename the subject line of your original post so that it reflects the actual subject. How about "Redirecting a form submission with parameters". Once you have solved the issue, please go back your original post subject line and prefit it with [SOLVED] (forum guidelines http://drupal.org/forum-posting )

You have already asked about how to "How to display values after form post" here http://drupal.org/node/939158. Therefore, please be considerate of other folks in the community and rename your post so that it reflects the actual issue ("Redirecting a form submission with parameters") :)

I look forward to your corrected subject line and the results of your research :)

I am learning’s picture

Thanks Jason, I'll check as suggested by you, I'll also rename the subject line. I apologize for the duplicate post, I didn't get a solution on my earlier post so I thought it was ambiguous (I don't how successful I was this time but I got two responses including yours).

Regards

technicalknockout’s picture

Hello,

I have a similar issue and tried to trace the source code as per the steps described above, but still don't understand what the form api is doing. Here's the scenario:

After submitting a form, I want my users to be redirected if a checkbox is checked. That's it. So far my checkbox displays fine (using hook_form_alter() ) & my custom submit function is being called. Here's my function's code:

<?php
function my_custom_function($form, $form_state) {
	if (isset($form['redirect_checkbox']['#value']['redirect'])) {
		$form_state['redirect'] = 'node/add/organization';
	}
}
?>

Following the advice above:
1) I found that $form_state['redirect'] is used by drupal_redirect_form
2) ok, function a is drupal_redirect_form
3) ok, definition and source for function a found: http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_redi...
so great, I need to get my value into the second parameter, which is "An optional value containing the destination path to redirect to if none is specified by the form."
4) not sure what this means exactly 'find which API method is using'... as in it uses drupal_goto? that's the actual redirection if I understand correctly, but what I need to do is just make sure it get's the path I want as a parameter - which the code above is not accomplishing...
5) have been through the following api pages:
http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_proc...
http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_redi...
http://api.drupal.org/api/drupal/includes--form.inc/function/form_execut...
Some more clues:
- I added a watchdog to form_execute_handlers() to see what functions were called and got (in order of execution) : node_form_validate, node_form_submit, menu_node_form_submit, my_custom_function
- I also add a watchdog to see what parameters are going to drupal_redirect_form() and saw that it was being called with $form_state['redirect'] = 'node/222'

Any ideas why my $form_state['redirect'] statement doesn't work? Been through the api documentation up and down and also through Pro Drupal Development 2nd edition, which says in the section 'Redirecting the User':

The function that processes the form should set $form_state['redirect'] to a Drupal path to which the user will be redirected, such as node/1234. If there are multiple functions in the #submit property, the last function to set $form_state['redirect'] will win. If no function sets $form_state['redirect'] to a Drupal path, the user is returned to the same page (that is, the value of $_GET['q']). Returning FALSE from the final submit function avoids redirection.

I am totally confused. Sorry for all the rambling, but just trying to make it clear what the behavior I'm seeing is like. Could anybody help?

- - turpana.com - -

jason_gates’s picture

Hi,
Happy to help. The context of this original post is going to confuse everyone. Therefore can you please post your question in a new post.

I am going to guess that you are trying to perform a typical task. After a user submits a form you want Drupal to respond with a custom message (e.g. Thank you for submitting the form").

Please specify which version of Drupal you are using (e.g. Drupal 6 or Drupal 7). Sounds like you are modifying a an existing form? If that's right, could you please title your new post something like "Add a custom reply to an existing form".

To create the custom response you need to do perform several tasks:

  • Create a custom function that renders your message.
  • Create a url (path), so that when the user submits the form, Drupal redirects to your custom path. Once Drupal receives the request to the custom path (e.g. http://MYSITE/my_custom_message), you want Drupal to call you custom function (created in the prior step). Drupal mechanism for mapping url paths to functions is called the "Menu" facility. That term confused me, all the other environments I worked in referred to that as method routing or function mapping, etc.
  • Alter the existing form.
  • Determine whether you want to replace or append the existing form's list of submit handlers.

I am also guessing that you are new to programming with the Drupal API. Sorry if I am wrong. Basically you want to refer to a number of items:

Again, if you please start a new trail (new post), with a definitive subject line, I will be happy to elaborate further.

Hope that helps.
Jason

technicalknockout’s picture

Thanks so much for looking in. In reply to your message I've created a new thread:
http://drupal.org/node/1042908

I am using Drupal 6. Also, with respect to the first set of points you list:

  • I am not trying to display a custom message, I am trying to redirect the user to another form
  • I have a working drupal path ready, it is 'node/add/organization'
  • I am altering the form to add my new checkbox and add my custom function to the $form['#submit'] array
  • I am not trying to replace the other handlers as I still want the data to be processed by the regular node submission functions

As I mentioned above, I looked up and down the following api pages and the source code therein:
http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_proc...
http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_redi...
http://api.drupal.org/api/drupal/includes--form.inc/function/form_execut...
Also, I am using the watchdog() function to log the parameters that are being passed around, as detailed above.

If you have any advice, would be much appreciated.

- - turpana.com - -

I am learning’s picture

I don't if there is a better way but what workaround I used is that I build the query using the parameters (values) posted by the form and stored this query into a session variable before redirecting.

This has solved my problem but I'll implement and share if I get a proper solution.

Thanks