Form Submissions

Hello.

How can I display a specific page or table on another custom page (custom dashboard).
In this case, it’s Form Submissions in Bricks Builder…
I created a FORM from which the data is stored into From Submissions.

I would like to display only this table from Form Submissions on another page (custom dashboard), which will be customized for a specific client.

What would be the easiest way to achieve this?
Can I design my own “table” and call the “ID” field from the Form Submissions page.

Thanks folks

2 Likes

I have the same request as you.

I want to query form submissions and their data on my website for a dashboard.
Maybe it can be used from the query loop. If not then Hopefully this will be a future feature.

Hey @timmse have you seen this question? Are you aware if there is a way to achvie this? I am also in the need of the functionality. Thanks in advance! You Rock!

Hi guys,
Unfortunately, that’s impossible with the query loop (since the form submissions aren’t a WP post type). Please add it as an idea to the idea board: Idea board – Bricks

However, you can query the form submissions with a custom SQL query like this in a code element, which should give you at least a starting point. Thanks to chatGPT for providing me with 99% of the code :smiley:

<?php

global $wpdb;

// Table name with the correct prefix
$table_name = $wpdb->prefix . 'bricks_form_submissions'; // This ensures it uses the right prefix.

// Sample query: Get all rows from the table
$query = "SELECT * FROM $table_name";

// Execute the query
$results = $wpdb->get_results($query, ARRAY_A); // ARRAY_A returns the result as an associative array.

// Check if the query returned any results
if ( !empty($results) ) {
    // Process your results
    foreach ( $results as $row ) {
        echo 'Form ID: ' . $row['form_id'] . ' / ';
        echo 'Submission Date: ' . $row['created_at'] . ' / ';

        // Decode the form data JSON string into a PHP associative array
        $data = json_decode($row['form_data'], true);

        // Loop through the first-level keys to get the dynamic key (like c8502d)   
        foreach ($data as $key => $inner_array) {
        // Check if the 'value' exists in the inner array
            if (isset($inner_array['value'])) {
                $value = $inner_array['value'];
                echo 'Value: ' . $value . '<br>';
            }
        }
    // Add more fields as needed
    }
} else {
    echo 'No form submissions found.';
}
?>

Best,
timmse

2 Likes

Hi,
if you want to show a table with the name (label) form fields as columns this is the code, based on the one by @timmse .

With this code you have a shortcode that you can put on any page you want.

Maybe it’s not perfect but… hey, it just works.

function show_bricks_submissions_shortcode()
{
	global $wpdb;
	
	$table_name = $wpdb->prefix . 'bricks_form_submissions';
	$query = $query = $wpdb->prepare("SELECT * FROM $table_name order by created_at desc");
	$submissions = $wpdb->get_results($query, ARRAY_A);

	
	if (!empty($submissions))
	{
		echo '<table class="wp-block-table is-style-striped">';
		echo '<thead><tr>';
		echo '<th>Form ID</th>';
		echo '<th>Submission Date</th>';
	
		$all_fields = array();
		foreach ($submissions as $submission)
		{
			$query = $wpdb->prepare("SELECT meta_value FROM mvv_postmeta WHERE meta_key = '_bricks_page_content_2' AND meta_value LIKE %s", '%' . $submission['form_id'] . '"%');
    
			$meta_rows = $wpdb->get_results($query);

   			$form_meta = unserialize($meta_rows[0]->meta_value);

			$form_fields = $form_meta[0]['settings']['fields'];

			if ($form_fields && is_array($form_fields))
			{
				foreach ($form_fields as $field_data)
				{
					if (isset($field_data['type']))
					{
						$all_fields = [
							'id' => $field_data['id'],
							'label' => $field_data['label'],
						];
					}
				}
			}
            
		}

		foreach ($form_fields as $field) {
			echo '<th>' . $field['label'] . '</th>';
		}
		echo '</tr></thead><tbody>';
	
		foreach ($submissions as $submission) {
			echo '<tr>';
			echo '<td>' . $submission['form_id'] . '</td>';
			echo '<td>' . $submission['created_at'] . '</td>';
	
			$data = json_decode($submission['form_data'], true);

			foreach ($form_fields as $field) {
				echo '<td>';
				if ($data && is_array($data)) {
					$found = false;
					foreach ($data as $key => $field_data) {
    
						if (isset($field_data['type']) && $key == $field['id'] && isset($field_data['value'])) {
							echo esc_html($field_data['value']);
							$found = true;
							break;
						}
					}
					if (!$found) {
						echo ''; // empty cell
					}
				} else {
					echo ''; // empty cell
				}
				echo '</td>';
			}
			echo '</tr>';
		}
	
		echo '</tbody></table>';
	
	} else {
		echo 'No form submissions found.';
	}
		
}
	
add_shortcode('show_bricks_submissions', 'show_bricks_submissions_shortcode');