Hello All,
I am looking to implement a multi-step form. I've seen several examples of how to do this. What I'd like to do though, is perform a database query and/or action after each step - not just at the end. I imagine the flow working something like this:
1) User completes Drupal form
2) External data source queried with term entered in step 1
3) Query results returned to Drupal module and displayed to user
4) User selects one of these results via radio button
5) Another form is displayed to collect additional information regarding the record user chose in step 4
6) All collected data is submitted into a custom Drupal DB table.
Other site users who see this module would then view data collected via the form above
I could do something like this very easily outside of Drupal. Within Drupal, however, I struggle to see how this is possible within the structure needed to implement a multi-step form.
Does anyone have any experience they could share or point me to a working sample? Are there other options here I may not have considered?
Thank you for your time!
Comments
I don't think this is hard
I have not reviewed any multi-step form examples on the site, but every form submit action has an associated submit handler, and every time the form is submitted, you can certainly query the database in that submit handler. One possibility is that each form submit goes to a new URL defined in your module by hook_menu, and for each menu URL, you have a callback function which handles rendering the form for that step as well as the submit action for that form.
If you are directing the form submission to the same URL for each step, you would be tracking the form step in a hidden form field or session variable somehow. Again, you can simply determine what step you are currently in when the form is submitted and do your database work.
Let me know if this makes sense. If you have a form example you wold like to follow, post it here so I can see it.
I don't have a specific
I don't have a specific example yet. I've spent most of my time looking at the multi-step examples to figure out where the database calls would go.
Should I perhaps look for an example with multiple submit handlers?
I am giving you a sample
I am giving you a sample code, this code you can refer while implementing in your module.
Yo can maintain the each step of your form using $form_state['storage']['step'].
The same ways use can create multistep form.
Now the next thing is to store these form values in each steps.
Here's a solution for that:-
Like this you can create any number of multistep form.
Thank you, hemant, for your
Thank you, hemant, for your example. This is very close to what I'm looking for. One change though. Suppose I take the variables in case 1 and execute a select query on an external DB using those variables. I want to send the result set from this query back to the module and have the user choose one of these as the 2nd step.
So, in a nutshell, how do you pass query results to the next step?
Got My First Step Working
I got my first step to work. I have a simple single field form that asks for a last name. I then query my external DB and display a list of classes taught by the person entered
What I want to do next is "step-ify" this so the results returned display in a new form that allows the user to choose one via radio button. Once selected, a third form would appear collecting an additional 3 or 4 pieces of information. After that, I would submit the info I collected into a separate Drupal table.
Also, Is it possible to display text after a form submission without putting it in a drupal_set_message() call?
Bumping
Hoping for a reply to my last question!
Building A New Form In Step 2
I'm still somewhat confused by this. Can someone explain how I can execute a DB query in the 2nd step based on user input in step 1 and build a completely new form (radio buttons) from the results?
It seems like Drupal makes things extraordinarily complicated for something that should be extraordinarily simple
where are you updating the form step
You have posted sample code. Where are you updating the form step as the user submits the form?
this is a very quick stab at your code
Drupal does not make it hard to do things. In fact, your database query code shows that you are doing things the hard way by choice. You don't have to connect to the database, and you do not use the native PHP database functions, all of the database functionality is provided by drupal for you. You do indeed have to spend some time understanding how Drupal works, but it doesn't make things hard to do.
@justageek
Thanks for your response. Based on what you did, I now have the following:
When I execute this, the page loads, but no form shows up. I suspect I may have missed something in setting up the step code. At least its not a completely blank page like I was getting before!
Its not that I'm doing it this way by choice. I'm doing it because its the only way I know how and I've not yet come across better ways to connect to an external database. The fact that I need to pull, display, allow user to choose data, then pass their choice back to Drupal is, in my mind, complicating things greatly. I'm used to being able to do anything at will in a PHP page. Needing to "Drupalize" my code is something I'm still *obviously* trying to get used to.
If I can get a handle on how to display database results as another form and pass that choice to the next step, I think I'll be in good shape.
Thanks again for your time.
try this...
Also, I don't follow what you are trying to do, I thought you were going to draw a different form in step 2. Where are you wanting to show the classlist, in the form for step 2?
this version is slightly different
The step 2 submit should forward to a new page that will show the class list, you will need to clear your menu cache.
this one is a little different
it should forward to a page after step 2 submit that shows the class list.
Sorry For The Delay...
@justageek: To answer your question, yes, I wanted to take the results I built in $classlist and return it as a form in the second step. I then execute another DB query based on what the user chooses in step 2.
Printing $classlist to the screen in a table is just to make sure the results were what I wanted. I need to change $classlist so they print out as form elements instead. I assume I need to do something like this:
Would that be accurate?
I tried your examples
@justageek
I tried a couple of your examples. The first one dated May 23, 2012 at 6:06pm gave me a 502 Bad Gateway error and the one dated May 23, 2012 at 6:13pm worked as far as the redirect but the results don't display. It doesn't look like they were being passed at all.
sorry
I've been swamped lately. If you copied my code directly, then it will probably not work. The function names have to be named properly in your module, and I don't know if my function names were good based on whatever you have named your module. The 502 error sounds like a server issue. If you can post your entire module code, so I can see your function names. Also, did you create the database table by hand?