Populating a second form from data saved in the database

This documentation page is for Quform version 1 and may not be applicable for Quform 2 click here to visit the documentation for Quform 2.

In this guide we will have a form which will gather some information from the user, generate a booking ID and save the submitted data to a custom database table. We’ll also have a second form which will be populated by the submitted data when the booking ID is given in the URL.

Step 1

Add a Hidden field to the first form, give it a label such as “Booking ID” and tick Dynamic default value and enter the Parameter name: uid

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
4
5
function my_get_unique_id($value)
{
    return uniqid();
}
add_filter('iphorm_element_value_uid', 'my_get_unique_id');
function my_get_unique_id($value)
{
    return uniqid();
}
add_filter('iphorm_element_value_uid', 'my_get_unique_id');

Step 2

Create the database table to store the intermediary data. In this example, the first form has 6 fields:

  • Booking ID (Hidden)
  • Name (Single Line Text)
  • Email (Email Address)
  • Message (Paragraph Text)
  • Start Date (Date)
  • End Date (Date)

So this is the database table structure I’ve ended up with.

Step 3

In this step we will add the code to save the data from the first form to the table you created in Step 2. Add the code below and configure it as described below the code.

1
2
3
4
56
7
8
9
10
11
1213
14
15
16
17
18
19
2021222324
25
26
27
28
function my_save_booking_data($form)
{
    global $wpdb;
 
    $startDate = $form->getElement('iphorm_1_5');    if ($startDate->isEmpty()) {
        $startDate = '';
    } else {
        $startDate = date('Y-m-d', strtotime($startDate->getValuePlain()));
    }
 
    $endDate = $form->getElement('iphorm_1_6');    if ($endDate->isEmpty()) {
        $endDate = '';
    } else {
        $endDate = date('Y-m-d', strtotime($endDate->getValuePlain()));
    }
 
    $wpdb->insert('bookings', array(
        'id' => $form->getValuePlain('iphorm_1_4'),        'name' => $form->getValuePlain('iphorm_1_1'),        'email' => $form->getValuePlain('iphorm_1_2'),        'message' => $form->getValuePlain('iphorm_1_3'),        'start_date' => $startDate,
        'end_date' => $endDate
    ));
}
add_filter('iphorm_post_process_1', 'my_save_booking_data');
function my_save_booking_data($form)
{
    global $wpdb;

    $startDate = $form->getElement('iphorm_1_5');
    if ($startDate->isEmpty()) {
        $startDate = '';
    } else {
        $startDate = date('Y-m-d', strtotime($startDate->getValuePlain()));
    }

    $endDate = $form->getElement('iphorm_1_6');
    if ($endDate->isEmpty()) {
        $endDate = '';
    } else {
        $endDate = date('Y-m-d', strtotime($endDate->getValuePlain()));
    }

    $wpdb->insert('bookings', array(
        'id' => $form->getValuePlain('iphorm_1_4'),
        'name' => $form->getValuePlain('iphorm_1_1'),
        'email' => $form->getValuePlain('iphorm_1_2'),
        'message' => $form->getValuePlain('iphorm_1_3'),
        'start_date' => $startDate,
        'end_date' => $endDate
    ));
}
add_filter('iphorm_post_process_1', 'my_save_booking_data');
  • On line 5 change iphorm_1_5 to the unique ID of the Start Date element
  • On line 12 change iphorm_1_6 to the unique ID of the End Date element
  • On line 20 change iphorm_1_4 to the unique ID of the Booking ID hidden element
  • On line 21 change iphorm_1_1 to the unique ID of the Name element
  • On line 22 change iphorm_1_2 to the unique ID of the Email element
  • On line 23 change iphorm_1_3 to the unique ID of the Message element
  • On line 28 change the number 1 to the form ID

Step 4

Create the second form if you haven’t done so already, you should at least have the same fields of the same type as the first form. Add the Hidden field for the Booking ID so you can keep track of it, but you shouldn’t enable the Dynamic default value option for this field in the second form.

Step 5

In this step we will add the code to grab the saved data from the database when the URL is passed a booking_id attribute that matches an existing row in the table. Add the code below and configure it as described below the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27282930313233
34
35
36
function my_load_booking_data($form)
{
    global $wpdb;
 
    $id = isset($_GET['booking_id']) ? $_GET['booking_id'] : '';
 
    if (!$id) {
        return;
    }
 
    $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM bookings WHERE id = '%s'", $id));
 
    if (is_object($result)) {
        $startDate = '';
        if ($result->start_date) {
            $startDate = explode('-', $result->start_date);
            $startDate = array('day' => (int) $startDate[2], 'month' => (int) $startDate[1], 'year' => (int) $startDate[0]);
        }
 
        $endDate = '';
        if ($result->end_date) {
            $endDate = explode('-', $result->end_date);
            $endDate = array('day' => (int) $endDate[2], 'month' => (int) $endDate[1], 'year' => (int) $endDate[0]);
        }
 
        $form->setValues(array(
            'iphorm_2_4' => $result->id,            'iphorm_2_1' => $result->name,            'iphorm_2_2' => $result->email,            'iphorm_2_3' => $result->message,            'iphorm_2_5' => $startDate,            'iphorm_2_6' => $endDate        ));
    }
}
add_filter('iphorm_pre_display_2', 'my_load_booking_data');
function my_load_booking_data($form)
{
    global $wpdb;

    $id = isset($_GET['booking_id']) ? $_GET['booking_id'] : '';

    if (!$id) {
        return;
    }

    $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM bookings WHERE id = '%s'", $id));

    if (is_object($result)) {
        $startDate = '';
        if ($result->start_date) {
            $startDate = explode('-', $result->start_date);
            $startDate = array('day' => (int) $startDate[2], 'month' => (int) $startDate[1], 'year' => (int) $startDate[0]);
        }

        $endDate = '';
        if ($result->end_date) {
            $endDate = explode('-', $result->end_date);
            $endDate = array('day' => (int) $endDate[2], 'month' => (int) $endDate[1], 'year' => (int) $endDate[0]);
        }

        $form->setValues(array(
            'iphorm_2_4' => $result->id,
            'iphorm_2_1' => $result->name,
            'iphorm_2_2' => $result->email,
            'iphorm_2_3' => $result->message,
            'iphorm_2_5' => $startDate,
            'iphorm_2_6' => $endDate
        ));
    }
}
add_filter('iphorm_pre_display_2', 'my_load_booking_data');
  • On line 27 change iphorm_2_4 to the unique ID of the Booking ID hidden element
  • On line 28 change iphorm_2_1 to the unique ID of the Name element
  • On line 29 change iphorm_2_2 to the unique ID of the Email element
  • On line 30 change iphorm_2_3 to the unique ID of the Message element
  • On line 31 change iphorm_2_5 to the unique ID of the Start Date element
  • On line 32 change iphorm_2_6 to the unique ID of the End Date element
  • On line 36 change the number 2 to the form ID

Step 6

Now if you submit the first form, you’ll get a booking ID shown in the email (or when viewing the entry within WP) for the Hidden field. It’ll look something like 530b2b113e417. If you visit the second form page, and add this ID in the URL like so, it’ll populate the form data.

http://www.example.com/second-form?booking_id=530b2b113e417

Step 7 (optional)

If you want a link to the second form with the booking ID built into it, add this code.

1
2
34
5
6
7
8
9
function my_custom_email_data($mailer, $form, $attachments)
{
    $url = 'http://www.example.com/second-form?booking_id=' . urlencode($form->getValue('iphorm_1_4'));    $mailer->Body .= '<p>Booking URL: <a href="' . $url . '">' . $url . '</p>';
 
    // You must return the $mailer object
    return $mailer;
}
add_filter('iphorm_pre_send_notification_email_1', 'my_custom_email_data', 10, 3);
function my_custom_email_data($mailer, $form, $attachments)
{
    $url = 'http://www.example.com/second-form?booking_id=' . urlencode($form->getValue('iphorm_1_4'));
    $mailer->Body .= '<p>Booking URL: <a href="' . $url . '">' . $url . '</p>';

    // You must return the $mailer object
    return $mailer;
}
add_filter('iphorm_pre_send_notification_email_1', 'my_custom_email_data', 10, 3);
  • On line 3 change http://www.example.com/second-form to the URL of the second form
  • On line 3 change iphorm_1_4 to the unique ID of the Booking ID hidden element
  • On line 9 change the number 1 to the form ID

Note: If you are testing multiple booking submissions – the booking ID is generated when the first form page is loaded, so you will need to refresh the page after submitting the first booking to generate a new booking ID or the second booking data will not be saved.

Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy