Display Form Data
Almost every website has its own forms, such as contact form, book form, and registry form... Instead of just submitting the form through email, we want to store the form data for the future inconvenience. Here's an example usage of it.
Online forms
Display form data for certain users
Contact Form DB plugin (version >2.4.1)
Contact Form 7 is a useful and easy-to-use WordPress plugin. However, after it sends an email the information disappeared. A plugin called "Contact Form DB" can help persist the form data and provide a shortcode to display the data in a data grid format.
Once the "Contact Form DB" is activated, it created two data tables: wp_cf7dbplugin_st (storing time key) and wp_cf7dbplugin_submits (storing form data).
In WordPress Dashboard, click on the Contact Form DB button on the left side menu. It would show all form data in a table.
Under the Contact Form DB button, there is a Shortcode button, it allows you to generate a shortcode of your choice. You could specify a form and the display format and columns to display.
The following shortcodes are provided:
- [cfdb-datatable]
- [cfdb-table]
- [cfdb-json]
- [cfdb-value]
- [cfdb-count]
- [cfdb-html]
Shortcode details
Basic Shortcode
You have a form named “myform
” and you want to display all its rows and columns in a page or post:
[cfdb-table form="myform"]
Choose Columns to Display with “show” and “hide”
[cfdb-table form="myform" show="f1,f2,f3"]
(optionally show selected fields)[cfdb-table form="myform
" hide="f1,f2,f3"]
(optionally hide selected fields)
“show” and “hide” supports regular expressions to be able to specify column names. Regular expression match according to preg-match.
Examples:
[cfdb-table form="myform" show="/.*name/"]
(shows all fields whose name ends with “name” such as “first-name” and “last-name”)
[cfdb-table form="myform" show="/.*name/i"]
(Same as previous, but is a case-insensitive match. So “Last-Name” as well as “last-name” match)
[cfdb-table form="myform" hide="/Submitted.*/"]
(Don’t show fields starting with “Submitted”)
[cfdb-table form="myform" hide="message,/Submitted.*/,/Cookie.*/"]
(Don’t show (1) the “message” field, (2) fields starting with “Submitted”, (3) fields starting with “Cookie”. If you have the option on to capture users’ Cookies with their form submissions, use hide=”/Cookie.*/” to hide them. )
- Remember that is you have both “show” and “hide” specified, any field that match both will be hidden. That is, “hide” trumps “show”.
Specify who can view
The “role” attribute can limit the short code to only create output for logged-in users with a minimal user role. Set the “role” attribute to one of:
- Administrator
- Editor
- Author
- Contributor
- Subscriber
- Anyone (users do not have to be logged in to see the short code output)
Example: if role="Editor"
is used, then a logged in Author, Contributor or Subscriber sees no output. A logged in Editor or Administration will see the output.
Notes:
- Case Counts. Be sure to capitalize the first letter of the role names
- If you set
permissionmsg="true"
then an error message will show on your page when a user does not have permission. By default, if “role” is being used, no permission error is shown.
Limit Rows to Display
Say you just want the first 10 rows, not every form submission. Use “limit”
[cfdb-table form="myform" limit="10"]
(shows only the first 10 rows)
[cfdb-table form="myform" limit="20,10"]
(shows only the first 10 rows starting with row #20. In other words, this shows rows #20-29. The previous example shows rows #10-19. So this is like the next page of 10 rows.
Sort data
[cfdb-table form="myform" orderby="lastname desc,firstname desc"]
(Reverse-sort by lastname and for those with the same lastname, reverse-sort by firstname)
You can use “asc” for ascending sort or “desc” for descending sort. But “asc” is assumed if not specified.
Which Rows to Display
The search field acts like the dynamic Search field that appears on the DataTable. It selects rows where any cell contains the search text (case in-sensitive).
[cfdb-table form="your-form" search="Washington"]
(show only rows where any field contains “Washington” . Same as "columnName like '%Washington%'" in where clause in SQL.)
Filter provides a much more specific and complex way to specify rows to show. Unlike “search”, “filter” specifies searches on specific columns.
[cfdb-table form="your-form" filter="field1=value1"]
(show only rows where field1=value1)[cfdb-table form="your-form" filter="field1=null"]
(SPECIAL CASE: ‘null’ is interpreted as null-value (field does has no value)[cfdb-table form="your-form" filter="field1!=value1"]
(show only rows where field1!=value1)[cfdb-table form="your-form" filter="field1=value1&&field2!=value2"]
(Logical AND the filters using ‘&&’)[cfdb-table form="your-form" filter="field1=value1||field2!=value2"]
(Logical OR the filters using ‘||’)[cfdb-table form="your-form" filter="field1=value1&&field2!=value2||field3=value3&&field4=value4"]
(Mixed &&, ||. Standard Boolean operator precedence applies (ANDs are evaluated, then ORs)[cfdb-table form="your-form" filter="field1~~/^a/"]
(Regular expression; shows rows where field1 starts with ‘a’)
Filtering Rows: Supported Filter Operators
=
and==
are the same!=
,<>
are the same>
,<
,<=
,>=
===
and!==
~~
for regular expressions- [in], [!in] for “in list” such as:
filter"=lastname[in]Simpson,Smith,Jones"
Filtering Rows: Filter by Regular Expressions
- Use the
~~
operator, and Perl-style delimiters around the pattern, such as/
[cfdb-table form="your-form" filter="field1~~/^a/"]
(shows rows where field1 starts with ‘a’)[cfdb-table form="your-form" filter="field1~~/.*@gmail.com/i"]
(shows rows where field1 is a Gmail address, case-insensitive)- FYI: uses preg_match to evaluate the regex
Filtering Rows: How to make “filter” Work Like “search”
Given “search” example:
[cfdb-table form="your-form" search="Washington"]
(Searches ALL fields)
When using “filter” use regular expressions with case-insensitive option (“i”), listing all fields.
[cfdb-table form="your-form" filter="field1~~/.*washington.*/i||field2~~/.*washington.*/i"]
(searches field1 and field2. You have to put a regex search for all the fields you want to search when using “filter”, strung together with logical ORs “||” )
Filtering Rows: Filter Limitations
- Does not support parentheses to control the order of boolean evaluation
Filtering based on Relative Time Using submit_time
For the “submit_time” field only, you can use special time values like:
[cfdb-table form="your-form" filter="submit_time>last week"]
(shows all submissions since last week)
More information
For more detail information on shortcode building for Contact Form DB read official site.
Comments