Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

What is Wordpress best practice for multi-step form development? WordPress

  • SOLVED

Hi,

I need to develop a multistep form plugin which does not rely on AJAX (though can be progressively enhanced). The type of functionality I require is a test with several questions attached to an post/page. I am aware that there are plugins out there but they do not fit my use case.

My question is what would be the smartest way to handle the saving of the form submission across multiple pages and to maintain state, using built-in wordpress functionality. The forms will be attached to a post using a shortcode. Wordpress version is 3.0.4 (must also work in Multisite)

I appreciate this is a fairly general question, I am just looking for pointers and any gotchas rather than code in order to kick-start development. To reiterate, the core functionality of stepping through the form must work without javascript so a page refresh will be required on submission of each step.

Thanks!

Answers (6)

2011-01-19

John Cotton answers:

Hi Luke

Well, you're going to need an id to track submissions across pages: PHP $_SESSION should deal with that.

The WordPress [[LINK href="http://codex.wordpress.org/Function_Reference/wpdb_Class"]]wpdb class[[/LINK]] is your friend for storing all the data and I would imagine that a custom table is in order as that would enable your to store precisely what you want and retrieve it in the way that you want.

I think you're suggesting that it needs to be ajaxable too, so code should be in a separate php file for easy inclusion with WordPress's ajax set up.

You'll need to pick your hook for grabbing posted data - parse_request perhaps?

On the multisite issue, you need to decide whether all data is stored in global tables or blog-level tables - very use-case dependant - but a bit of a gotcha for first-time plug-in developers.

Have you looked at [[LINK href="http://www.gravityforms.com/"]]Gravity Forms[[/LINK]]? It's got a very long feature list...

John

2011-01-19

rilwis answers:

I would save temporary form values in each step in <em>an option</em>. The method is like this:

// register an option

$forms = array();
add_option('temp_form', $forms);

This variable will have the following structure:

$forms = array(
// form id = 1
1 => array(
'name1' => 'value1' // and so on
),
// form id = 3
3 => array(
'name3' => 'value3' // and so on
),
);


In each step of the form, we declare "form ID" (which will be used in $forms variable), and "step", the url will look like this:

domain.com/page?fid=1&step=1

In the page, you can get current form values:

$forms = get_option('temp_form');
$id = $_GET['fid'];
$form = $forms[$id];

if ($_GET['step'] == 1) {
// show element for this step
}

// then update if needed
$form['new_name'] = $_POST['new_name']; // of course validation and error messages can be set here
$forms[$id] = $form;
update_option('temp_form', $forms);


To keep the current form working even when user close tab, you can save the form ID and step in $_SESSION. To keep it working even when user close browser, cookie is the choice. Or you can save some basic user's information (like username if he's logged in) in the form values, and when the page is loaded, you need to get the correct form based on this value.

Hope this can help you. An interesting question :)

2011-01-19

Andrzej answers:

Hi Luke,

I have done it by implementing quite comprehensive [[LINK href="http://www.deliciousdays.com/cforms-plugin/"]]cformsII plugin[[/LINK]]. Try it out.

It allows you to create multi-step forms, both ajax and non-ajax ones. It's generally extemely configurable and quite easy to content manage. You can add all sorts of input fields, set validation rules via CMS, you can even do completely custom logic (say user selected answer A, move him instantly to question 5); by custom functions.php file in plugin.

Forms can be embedded inside post/pages via shortcode and results can be sent via email or collected in the back-end.

Hope this helps,
Andrzej


Luke Mackenzie comments:

Sorry, this doesn't answer the question. I need to develop my own custom plugin.


Andrzej comments:

Ah.. sorry, misunderstood your question.

Well, I'd say you might want to look into that plugin anyway and see how its handled there. Its there from a longer while so probably they developed decent practices is that time.

From what I observed, they don't keep it in next steps hidden input, neither $_POST - so I believe it must be some temporary table in WP database.

Hopefully that's at least part-answer :). I'll send you a private message with front-end example of form I did using it - might wanna check yourself.

2011-01-19

Peter Michael answers:

Depends a little. So, for your 'fairly general question', I give a 'fairly general answer':

If you are handling anonymous users, I'd go with sessions and cookies. Try to ID the users somehow, maybe they have to enter email address & name somewhere.

If you are handling authenticated users, I'd store/append the submitted data in a user meta object.

HTH

2011-01-19

Monster Coder answers:

Hi,
Few days ago I have worked on such a problem with wordpress. I needed to handle 4 step form submission and even users can come back and forth (even in future).

I did not wanted to use Session. Then I used another solution and it is so far reliable.

It was actually creating a wordpress post in multiple steps. Each step users adds contents, images, etc. So this is how I did it:

1. after first step, I have created a draft post and put the current values in the post. some value is in custom field.
2. in second step, I made other contents.
....
....
4. In final step, I have saved these currents same as last 3 steps. and after saving everything I made the post 'publish'.

There may be some unfinished draft posts. You can also handle them easily:

1. Before publishing keep them in a temporary category. So you can occasionally delete all posts from that category
2. you can make a plugin to automatically delete unfinished draft posts (you can put a custom field too to identify such posts)

I hope you can follow this too if you do not see any smarter way. Please share if you find too :).

Thanks

2011-01-21

Keith Fawcett answers:

Luke,

You should really check out [[LINK href="http://www.gravityforms.com/"]]Gravity Forms[[/LINK]].

- It can do multi-page/multi-step forms without AJAX. It also has a stepped breadcrumb to show user how far along they are in the form.
- It has Short codes to attach forms to pages or posts.
- It works with multi-site.


It also has many other features like (these require a developer license though):
- A paypal add-on
- A registration add-on to create custom registration pages (still in beta), but works great. - I'm running it on my site.
- A mail chimp add-on
- A fresh books add-on

Their support is great