How to get submitted data from Contact form 7

In this example i will show you how to get submitted data from database. It is usefull when you are trying to create a report based on submitted data.

First of all we need these plugins:

Steps that we will follow:

Step 1:

  • Declare global $wpdb variable;
  • Write a SQL statement;
  • Get data from database using wpdb get_results method;

Step 2:

  • Loop received data
  • Unserialize submitted data

I created a form and entered some data.

<label> Your Name (required)
    [text* your-name] </label>

<label> Your Country (required)
    [select* country include_blank "Germany" "Spain" "United Kingdom"] </label>

[submit "Send"]

All data is stored in ” wp_db7_forms ” table and look like this:

form_id – unique id for each submission;
form_post_id – specific form id;
form_value – submitted data;
form_date – created date;;

Most important columns are “form_post_id” and “form_value” we will use them later.

Time to write a code:

STEP 1. We must declare global $wpdb variable and then write SQL query.

<?php

    global $wpdb;

    $sql = 'SELECT * FROM wp_db7_forms WHERE form_post_id = 5 ORDER BY form_id DESC';

    $results = $wpdb -> get_results($sql);

?>

STEP 2. Loop our results and most important unserialize data. To do it we will use unserialize() function:

<?php

    foreach ($results as $result) {

        $values = unserialize($result -> form_value);

    }

?>

Final code with HTML tags:

<?php

    global $wpdb;

    $sql = 'SELECT * FROM wp_db7_forms WHERE form_post_id = 5 ORDER BY form_id DESC';

    $results = $wpdb -> get_results($sql);

?>

<table>

    <tr>

        <th>Date</th>
        <th>Name</th>
        <th>Country</th>

    </tr>

    <?php foreach ($results as $result): ?>

        <?php $values = unserialize($result -> form_value); ?>

        <tr>

            <td><?= $result -> form_date; ?></td>
            <td><?= $values['your-name']; ?></td>
            <td><?= $values['country']; ?></td>

        </tr>


    <?php endforeach; ?>

</table>