Simplify copying Ninja Forms field values between fields early in the submission.
After being a consistent user of Contact Form 7 plugin for many years I changed to Ninja Forms early last year because CF7 wasn’t able to handle a Product Enquiry form for Variable WooCommerce Products (and it was slow).
For a long while I was frustrated that the data submitted via the dynamic dropdown was not appearing in the Submissions area. Earlier this year I solved this issue and now I can display dynamic drop down selection in Submissions.
I recently investigated copying the selected item into another field that would simplify things. The plan was to add a hidden field to the form and copy the selected dropdown item value to it.
The Perfect Filter
The ‘ninja_forms_submit_data‘ filter runs before any of the form actions run (Store Submission, Success Message, Email Confirmation, Email Notification). It includes the submitted fields data – the (internal) ID number, the submitted value and then the field key.
I wrote some code to experiment with this (to copy the email address field value into the hidden field value). I used the Field Keys as set in the Administration section for each field. The data was correctly saved with the Submission data and included in the emails.
I then wrote a class to simplify the copying. You can pass an array of field names to copy from and to. This is done via a filter. I used a class because I was felt that I needed member variables for the form ID and the array of fields to copy. After some testing I deleted the form ID variable as it was not required – I pass it to the filter function and the developer can adjust the array of fields to copy based on the form ID.
The form data is a multi level array so code to copy the fields quickly gets messy. My code creates an array matching field keys to field IDs. This lookup array is used when copying the fields. If the fields to be copied are invalid then that pair are not copied.
Using the class
This example shows how I return different data depending on the form ID.