Design a Multi-Page Form in WordPress: Data Storage

Share this article

In this second article in our series on multi-page form design, we’re going to create the database tables required to store the custom form data. We’ll also delve into the process of storing the first page of form data and displaying the second page to the user. We employ some very basic PHP and SQL to achieve our design goals. (If you’re interested in learning more about PHP, our partner site, PHPMaster.com, has a wide variety of guides ranging from beginner to expert.)

Step 1: Using phpMyAdmin to Create a Database Table

If you’ve never worked with phpMyAdmin, this is a big step for you. I realize that it can be daunting, but custom form development usually means you’re going to want custom database tables. While you could use existing, built-in WordPress data tables and store this information as user meta, you’re going to have to jump through many more hoops to make that work. In the end, avoiding phpMyAdmin is a lot harder than learning it. So, go to your domain hosting provider, log in, and go to your hosting control panel. You should see a button or link for phpMyAdmin or some other database management tool. Since the vast majority of domain hosting providers use phpMyAdmin, I’ll use that as my example. Once logged into phpMyAdmin, go to the SQL tab for your WordPress installation and paste in the following code: [sourcecode language=”sql”] CREATE TABLE `shopping_preferences` ( `id` INT( 7 ) NOT NULL AUTO_INCREMENT, `first_name` VARCHAR( 50 ) NOT NULL, `last_name` VARCHAR( 50 ) NOT NULL, `email` VARCHAR( 50 ) NOT NULL, `phone` VARCHAR( 12 ) NOT NULL, `zip_code` VARCHAR( 5 ) NOT NULL, `gender` INT( 1 ) NOT NULL, `age` INT( 3 ) NOT NULL, `education` VARCHAR( 50 ) NOT NULL, `income` VARCHAR( 50 ) NOT NULL, `location` VARCHAR( 50 ) NOT NULL, `categories` VARCHAR( 400 ) NOT NULL, `page` INT( 1 ) NOT NULL, `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY ( `id` ) ) [/sourcecode] You can modify this code as needed, of course, but this will get a new data table in place and allow you to start adding content from the inputs of our multi-page form.

Step 2: Adding Page One Data

For this step, we’ll accomplish two things:
  1. Send the page one form inputs into the database table that we created in Step 1
  2. Retrieve the ID of the form data so we can keep adding more information as the user fills out the forms.
To add data into our database table, we’ll be using the built-in WordPress $wpdb. While you can create a MySQL INSERT script, it’s good practice when working with WordPress databases to use their carefully-designed functionality. It’s a simple process that is also the least intuitive ever… at first. Once you get the hang of working with $wpdb, you’ll be just fine. First, we need to grab the POST data from page one of our form. If you’ve ever worked with forms, this is a familiar process. The insert process starts by defining the columns using an array format assigned to a variable. Then the insert function takes it from there. [sourcecode language=”php”] // Start Page 2 of Form elseif ( $page == 1 ) { // Grab the POST data that the user provided $first_name = $_POST[‘first_name’]; $last_name = $_POST[‘last_name’]; $email = $_POST[’email’]; $phone = $_POST[‘phone’]; $zip_code = $_POST[‘zip_code’]; // Assign the table and inputs for our upcoming INSERT function $page_one_table = ‘shopping_preferences’; $page_one_inputs = array( ‘first_name’ => $first_name, ‘last_name’ => $last_name, ’email’ => $email, ‘phone’ => $phone, ‘zip_code’ => $zip_code, ‘page’ => $page ); // Insert the data into a new row $insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs); // Grab the ID of the row we inserted for later use $form_id = $wpdd->insert_id; echo ‘<h3>You made it to the 2nd page!</h3> Here are your form inputs: First Name: ‘ . $first_name . ‘ Last Name: ‘ . $last_name . ‘ Email: ‘ . $email . ‘ Phone: ‘ . $phone . ‘ Zip Code: ‘ . $zip_code . ‘ Form ID: ‘ . $form_id . ”; }//End Page 2 of Form [/sourcecode] In the last part of the code, we are doing a bit of initial checking of our data, we display our message about making it to page two of the form, and then we show the stored input values to the user who provided them. If we have a Form ID value, we have successfully inserted a row!

Step 3: Adding the Page Two Form

So, we inserted a row of data from our first page of the form, and now we’re ready to collect some more information. This time, we want to get some socioeconomic data. Even if the user bails on us at this point, we’ve still got some useful information that we can use to get in touch with them later. After the $form_id code above, we’re going to replace the rest and add the second page of our fancy form: [sourcecode language=”php”] echo ‘<form method=”post” action=”‘ . $this_page .'”> <label for=”gender” id=”gender”>Gender: </label> <select name=”gender” /> <option value=”nothing” selected>Select Gender</option> <option value=”0″>Female</option> <option value=”1″>Male</option> </select> <label for=”age” id=”age”>Age: </label> <input type=”text” name=”age” id=”age” /> <label for=”education” id=”education”>Education: </label> <select name=”education” /> <option value=”nothing” selected>Select Level of Education</option> <option value=”some_high_school”>Some High School</option> <option value=”all_high_school”>High School Diploma/GED</option> <option value=”some_college”>Some College</option> <option value=”all_colleg”>College Degree</option> <option value=”some_grad”>Some Graduate School</option> <option value=”all_grad”>Graduate</option> <option value=”some_doc”>Some Post Graduate</option> <option value=”all_doc”>Doctorate</option> </select> <label for=”income” id=”income”>Income: </label> <select name=”income” /> <option value=”nothing” selected>Select Income Range</option> <option value=”10000″ selected>Less than $10,000</option> <option value=”25000″ selected>$10,000 – $25,000</option> <option value=”50000″ selected>$25,000 – $50,000</option> <option value=”75000″ selected>$50,000 – $75,000</option> <option value=”max” selected>More than $75,000</option> </select> <input type=”hidden” value=”2″ name=”page” /> <input type=”hidden” value=”‘ . $form_id . ‘” name=”form_id” /> <input type=”submit” /> </form>’; } //End Page 2 of Form [/sourcecode] For the sake of brevity, I left the “Age” option as a fill in the blank so we don’t have a long form with overwhelming options. The final version will have a drop-down menu.

Step 4: Building the Page 3 Handler

Now, let’s grab the information from page two and make sure we’ve captured what we need. We’ll display it on the page for testing purposes. Another ELSEIF statement is required to test for the page number. Just place this immediately after the “End Page 2″ comment from the previous code sample: [sourcecode language=”php”] elseif( $page == 2 ) { $gender = $_POST[‘gender’]; $age = $_POST[‘age’]; $education = $_POST[‘education’]; $income = $_POST[‘income’]; $page = $_POST[‘page’]; $form_id = $_POST[‘form_id’]; echo ‘$gender: ‘ . $gender . ”; echo ‘$age: ‘ . $age . ”; echo ‘$education: ‘ . $education . ”; echo ‘$income: ‘ . $income . ”; echo ‘$page: ‘ . $page . ”; echo ‘$form_id: ‘ . $form_id . ”; } [/sourcecode] Make sure your function still has the closing “};” brace — it’s easy to copy and paste over the top of it. Missing one of these opening or closing braces or brackets can break your entire form, so work carefully.

Conclusion

Refresh your form and behold! We’re getting close! You’ve already got a two-page form that successfully collects data and stores it from page one to page two. That’s a huge first step. In the next article, I’ll show you how to update the database with page two inputs and how to display an optional version of the form — one for males and one for females. For the sake of completeness, here’s the code we have so far: [sourcecode language=”php”] add_shortcode(‘multipage_form_sc’,’multipage_form’); function multipage_form(){ global $wpdb; $this_page = $_SERVER[‘REQUEST_URI’]; $page = $_POST[‘page’]; if ( $page == NULL ) { echo ‘ <form action=”‘ . $this_page .'” method=”post”><label id=”first_name” for=”first_name”>First Name: </label> <input id=”first_name” type=”text” name=”first_name” /> <label id=”last_name” for=”last_name”>Last Name: </label> <input id=”last_name” type=”text” name=”last_name” /> <label id=”email” for=”email”>Email: </label> <input id=”email” type=”text” name=”email” /> <label id=”phone” for=”phone”>Phone: </label> <input id=”phone” type=”text” name=”phone” /> <label id=”first_name” for=”first_name”>Zip Code: </label> <input id=”zip_code” type=”text” name=”zip_code” /> <input type=”hidden” name=”page” value=”1″ /> <input type=”submit” /></form>’; }//End Page 1 of Form // Start Page 2 of Form elseif ( $page == 1 ) { $first_name = $_POST[‘first_name’]; $last_name = $_POST[‘last_name’]; $email = $_POST[’email’]; $phone = $_POST[‘phone’]; $zip_code = $_POST[‘zip_code’]; $page_one_table = ‘shopping_preferences’; $page_one_inputs = array( ‘first_name’ => $first_name, ‘last_name’ => $last_name, ’email’ => $email, ‘phone’ => $phone, ‘zip_code’ => $zip_code, ‘page’ => $page ); $insert_page_one = $wpdb->insert($page_one_table, $page_one_inputs); $form_id = $wpdb->insert_id; echo ‘ <form action=”‘ . $this_page .'” method=”post”><label id=”gender” for=”gender”>Gender: </label></form><select name=”gender”></select>Select GenderFemaleMale <label id=”age” for=”age”>Age: </label> <input id=”age” type=”text” name=”age” /> <label id=”education” for=”education”>Education: </label> <select name=”education”></select>Select Level of EducationSome High SchoolHigh School Diploma/GEDSome CollegeCollege DegreeSome Graduate SchoolGraduateSome Post GraduateDoctorate <label id=”income” for=”income”>Income: </label> <select name=”income”></select>Select Income RangeLess than $10,000$10,000 – $25,000$25,000 – $50,000$50,000 – $75,000More than $75,000 <input type=”hidden” name=”page” value=”2″ /> <input type=”hidden” name=”form_id” value=”‘ . $form_id . ‘” /> <input type=”submit” /> ‘; }// End Page 2 of Form // Start Page 3 of Form elseif( $page == 2 ) { $gender = $_POST[‘gender’]; $age = $_POST[‘age’]; $education = $_POST[‘education’]; $income = $_POST[‘income’]; $page = $_POST[‘page’]; $form_id = $_POST[‘form_id’]; echo ‘$gender: ‘ . $gender . ”; echo ‘$age: ‘ . $age . ”; echo ‘$education: ‘ . $education . ”; echo ‘$income: ‘ . $income . ”; echo ‘$page: ‘ . $page . ”; echo ‘$form_id: ‘ . $form_id . ”; };// End Page 3 of Form }// End multipage_form() function [/sourcecode]

Frequently Asked Questions about Designing a Multi-Page Form in WordPress and Data Storage

How can I create a multi-page form in WordPress without using a plugin?

Creating a multi-page form in WordPress without using a plugin requires some knowledge of PHP and HTML. You’ll need to create a custom form and split it into multiple pages using PHP sessions or cookies to store user data between pages. However, this can be complex and time-consuming, especially for beginners. Using a plugin like WPForms or Formidable Forms can simplify this process, allowing you to create multi-page forms with just a few clicks.

How can I store form data in the WordPress database?

Storing form data in the WordPress database can be done using the built-in WordPress function wpdb. This function allows you to interact with the database directly. You can use it to insert, update, delete, and retrieve data from your database. However, this requires a good understanding of SQL and the structure of your WordPress database. Alternatively, you can use a plugin that automatically stores form data in the database.

Can I retrieve and display form data from the WordPress database on my website?

Yes, you can retrieve and display form data from the WordPress database on your website. This can be done using the wpdb function to run a SELECT query on your database. The returned data can then be displayed using PHP. However, this requires a good understanding of PHP and SQL. If you’re not comfortable with coding, you can use a plugin that provides a user-friendly interface for retrieving and displaying form data.

How can I ensure the security of my form data in WordPress?

Ensuring the security of your form data in WordPress is crucial. You can do this by using prepared statements when interacting with the database to prevent SQL injection attacks. Also, always validate and sanitize user input to prevent cross-site scripting (XSS) attacks. If you’re using a plugin, make sure it follows these security best practices.

Can I export form data from the WordPress database to a CSV file?

Yes, you can export form data from the WordPress database to a CSV file. This can be done using the wpdb function to retrieve the data and PHP’s built-in functions to create and write to a CSV file. However, this requires a good understanding of PHP and SQL. Alternatively, many form plugins provide an export feature that allows you to easily export form data to a CSV file.

How can I create conditional logic in my multi-page form in WordPress?

Creating conditional logic in your multi-page form in WordPress can be done using JavaScript or jQuery. This allows you to show or hide form fields or pages based on the user’s input. However, this requires a good understanding of JavaScript or jQuery. If you’re not comfortable with coding, many form plugins provide a user-friendly interface for creating conditional logic.

Can I integrate my multi-page form with other services like MailChimp or Google Sheets?

Yes, you can integrate your multi-page form with other services like MailChimp or Google Sheets. This can be done using their respective APIs. However, this requires a good understanding of APIs and coding. Alternatively, many form plugins provide integrations with popular services, allowing you to easily connect your form to these services.

How can I style my multi-page form in WordPress?

Styling your multi-page form in WordPress can be done using CSS. You can add custom CSS to your theme’s style.css file or use the Customizer’s Additional CSS section. However, this requires a good understanding of CSS. If you’re not comfortable with coding, many form plugins provide a user-friendly interface for styling your form.

Can I create a multi-step form in WordPress?

Yes, a multi-step form is essentially the same as a multi-page form. The difference is mainly in the user interface. In a multi-step form, the steps are usually displayed in a progress bar, giving the user a clear indication of their progress through the form. Creating a multi-step form requires the same skills and tools as creating a multi-page form.

Can I use a multi-page form for user registration in WordPress?

Yes, you can use a multi-page form for user registration in WordPress. This can be useful if you need to collect a lot of information from the user. However, keep in mind that the user experience should be as smooth as possible. Don’t ask for unnecessary information and make sure the form is easy to navigate. You can use a plugin to create a custom user registration form with multiple pages.

Justyn HornorJustyn Hornor
View Author

When he's not being a complete goofball, “they” drag Justyn into the office where he pretends to be a Senior Editor and Content Engineer at Creative Content Experts — a content marketing firm out of NW Arkansas. He has 10+ years’ experience in technical writing and geek-related fields. He loves WordPress, coffee, and peanut butter a little too much.

multi-page formmultiple page formweb developmentWordPress
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week