Is it possible to save CF7 form submissions as PHP variables for front-end use?

Seeking assistance as I've encountered a roadblock and have been searching for a solution for the past 5 hours.

I've set up my CF7 form and implemented a function to store all fields as variables and display them in the error log, which is functioning properly:

function calculator_quote($contact_form){
  $title = $contact_form->title();

  if($title === 'Online Calculator'){
      $submission = WPCF7_Submission::get_instance();

      if($submission){
          $posted_data = $submission->get_posted_data();

          $cf7_fuel = $posted_data['fuel-quantity'];
          $cf7_postcode = $posted_data['your-postcode'];
          $cf7_delivery = $posted_data['delivery-type'];
          $cf7_tanker = $posted_data['tanker-type'];
          $cf7_email = $posted_data['your-email'];

          ob_start();
          var_dump($posted_data);
          error_log(ob_get_clean());
      }
  }
}

add_action('wpcf7_before_send_mail', 'calculator_quote' );

Now, I need to utilize these variables on the front-end to perform calculations (based on global ACF fields) using simple PHP sums and comparison queries tied to the form input. While aware that CF7 submissions are sent via AJAX, I lack knowledge on extracting and displaying them effectively. Storing isn't necessary, just outputting and visible below the form is required.

Your guidance on the best approach would be highly valued. Thank you.

Answer №1

Ensure your function delivers the post data

<?php
function send_back_data_from_form($contact_form){
  $title = $contact_form->title();

  if($title === 'Online Form'){
      $submission = WPCF7_Submission::get_instance();

      if($submission){
          $posted_data = $submission->get_posted_data();
          $array = [];
          $cf7_info1 = $posted_data['info-1'];
          $cf7_info2 = $posted_data['info-2'];
          // Continue for other form fields as needed

          // Logging posted data for debugging
          ob_start();
          var_dump($posted_data);
          error_log(ob_get_clean());

          return json_encode($array);
      }
  }
  return false;
}

add_action('wpcf7_before_send_mail', 'send_back_data_from_form');
?>

then display it on the front-end beneath your form

<script>
var formData = <?php do_action( 'wpcf7_before_send_mail' ); ?>;

// Output data in the browser
document.write(formData);
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigate through the items in my subnavbar by scrolling the content

HTML Code <div id="subnavigation"> <?php $verbindung = mysql_connect("host", "user" , "pw") or die("Verbindung zur Datenbank konnte nicht hergestellt werden"); mysql_select_db("db") or die ("Datenbank konnte nicht ausgewählt w ...

When adding values, they remain in their original positions

I am populating a select dropdown with options using AJAX. These options are added based on the selection made in another dropdown. The first function that appends the data: var appendto_c = function(appendto, value, curcode) { appendto.append("& ...

In relation to an SEO Search and special HTML characters

I'm currently working on a website that offers users the ability to search through our site database. The search script is customized, using "match" against in SQL. Here's how it works: The user inputs text (which can include numbers and specia ...

The wordpress plugin I tried using to link our contact form with InfusionSoft CRM was "Contact Form 7 - InfusionSoft Add-on," but unfortunately, it did not successfully send the contact form mail to InfusionSoft

I'm trying to save contact form submissions from Contact Form 7 to my Infusionsoft CRM using the Contact Form 7 - InfusionSoft Add-on WordPress plugin. I've added my Infusionsoft API key and App name, but unfortunately it's not functioning a ...

When Infinite Scroll is integrated into another file with HTML tags stacked on top, it will not load additional posts when scrolling down

I have implemented an Infinite Scroll feature that dynamically loads more data from a database as users scroll to the bottom of the page. However, I encountered an issue when trying to include this functionality in another .PHP file. If I insert any HTML ...

What can be done to ensure that the a href tag is functioning as clickable?

Having an issue with my HTML and CSS code for a notification dropdown box. I am unable to click the tag, even after attempting to use JavaScript. Can't seem to figure out what's causing this problem. Any advice on how to make the tag clickable? ...

What is the functionality of the "async:false" setting in a jQuery AJAX request?

I understand the purpose and usage of {async:false} in jQuery AJAX requests. However, I am curious to know how this operates synchronously. What is the mechanism behind it? ...

Validation of YML file has been disregarded

I'm a newcomer to the Symfony 2 web framework and facing challenges with a basic validation task. My entity model Post includes a member called slug, used for generating post links. In Post.orm.yml, I've set unique: true and aim to enforce this c ...

What steps can I take to troubleshoot the issue of missing data on my screen?

I have developed a custom WordPress plugin to fetch token data from PancakeSwap, but for some reason, the data is not displaying. I would appreciate it if someone could review my code and help me identify the issue. Thank you in advance. <?php /* Plu ...

Launching ng-app for AngularJS after the page has finished loading

After loading content with jQuery AJAX that includes ng-app="" and other directives, the AngularJS initialization does not occur. How can I initialize an ng-app that was added after the page has already been loaded? <script src="https://ajax.googleap ...

How to launch a new window for a popup

How can I make IE8 open new windows as pop-ups without changing browser settings? I want to use JavaScript's window.open() function. Firefox opens new windows correctly, but IE8 opens them in tabs instead of pop-up windows. Any suggestions on how to a ...

What are the steps for making Ajax calls?

I have been working on a Wikipedia viewer for my freecodecamp project. However, I am facing issues with the AJAX request as it keeps failing every time without returning any results. var url, value; $(document).ready(function() { $("button").on("click ...

Take the information entered in an HTML form, add it to a JSON object, and save it in a MYSQL

I'm currently working on extracting data from a user registration form in HTML, converting it to JSON format, and then storing it in MySQL. Your assistance with this process would be greatly appreciated. HTML <form id="myForm" action="userInf ...

Having trouble with my PHP regular expression to detect duplicated characters

Having trouble with basic php regex to identify repeated characters... $subject = 'rrrr'; var_dump(preg_match("/([a-zA-Z])\1{2,}$/i", $subject)); var_dump(preg_match("/(\w)\1{2,}$/i", $subject)); It seems to be working correctly ...

These constant syntax errors are really starting to drive me up the wall

I'm working on a code that starts at 10 and goes down to 1, with odd numbers being incremented by 1 and even numbers being decremented by 1. The code works fine without using ""<br />"", but when I try to line up the numbers individually my synt ...

To prevent the execution of a function when a variable is empty and show an error message using PHP and AJAX

Currently, I have a neat AJAX function set up to send data over to a .php file for processing: $(document).on('click','#submit',function () { var title = $('#title_submit').val(); var content = $('#content_submit ...

Error message occurs in jQuery form when optional fields are left empty, displaying a "missing ) after argument list" notification

I've encountered an issue with my form that submits to a jQuery plugin I developed. Until now, everything worked smoothly, but when I added optional fields to the form, things started going wrong. The input text fields in the form are initially popula ...

Using json_encode incorporates the table name into the JSON format

I am encountering an issue with encoding JSON data from my MySQL table "contacts". The data in the row looks like this: { "contactList": [ { "userId":"1062", "name":"Test User 1" }, { "us ...

Failure to showcase AJAX JSON data on DataTable

Issue with DataTable and AJAX JSON Data I have been encountering an issue while working on a project that involves using DataTable to display POST data via AJAX. The problem I am facing is that all the records are being displayed without pagination, even ...

Encountering issue with PHP/PDO while trying to add a record

I am encountering a challenge in creating a basic PHP record insertion script. I keep receiving an error message indicating a syntax issue. This particular task is for internal purposes only and will not be deployed on a live server, so concerns about inj ...