How to Dynamically Update Form Field Values Before Submission in Ninja Forms
Content
Many Ninja Forms users need to manipulate form data just before it's submitted, such as combining separate date fields into a single hidden field. A common approach is to use custom JavaScript with Marionette and Backbone.Radio, which are the libraries that power the Ninja Forms front-end. However, users often find their code doesn't execute as expected, with console logs appearing in the initialization phase but not within the submit function itself.
Why This Happens
The issue typically stems from using the wrong Radio channel event. The Ninja Forms submission process involves several events, and hooking into the correct one is crucial for your code to run at the intended time. The maybe:submit event might not be the appropriate hook for altering field values before the final submission data is packaged and sent.
Common Solutions
1. Use the 'submit:before' Event
Based on community findings and developer documentation, a more reliable channel for altering values immediately before submission is often the submit:before event. This event is triggered after validation but before the data is sent to the server.
var mySubmitController = Marionette.Object.extend( {
initialize: function() {
// Listen for the 'submit:before' event on the 'forms' channel
this.listenTo( Backbone.Radio.channel( 'forms' ), 'submit:before', this.beforeSubmit );
console.log('init');
},
beforeSubmit: function( response ) {
// Your logic to get and set field values
let day = jQuery( '#nf-field-60' ).val();
let month = jQuery( '#nf-field-61' ).val();
let year = jQuery( '#nf-field-62' ).val();
let dob = year +'-'+ month +'-'+ day;
jQuery( '#nf-field-69' ).val(dob);
console.log('Hidden field value set to: ' + dob);
}
});
// Instantiate the controller
new mySubmitController();
2. Ensure Proper Field Targeting
Double-check that the field IDs (e.g., #nf-field-69) in your JavaScript are correct. These IDs can sometimes change when a form is updated or duplicated. Using the correct, current ID is essential for the script to find and manipulate the field.
3. Verify JavaScript Loading and Scope
Ensure your custom JavaScript code is being loaded on the page where the form is displayed. It should be enqueued properly in WordPress after the Ninja Forms scripts have been loaded to ensure the Marionette and Backbone objects are available.
Alternative Approach: PHP Filter
For some data manipulation tasks, especially those that don't require immediate user-facing feedback, using a PHP filter on the server-side can be more reliable. The ninja_forms_submit_data filter allows you to alter the submission data array before it is processed by actions (like sending an email or saving to the database).
add_filter( 'ninja_forms_submit_data', 'my_custom_submit_data_manipulation' );
function my_custom_submit_data_manipulation( $form_data ) {
// Find your fields in the $form_data['fields'] array by their key or settings
foreach( $form_data['fields'] as $key => $field ) {
// Logic to find your specific fields and combine their values
// Example: if ( 'dob_day' == $field['key'] ) { ... }
}
// Return the modified data array
return $form_data;
}
By understanding the Ninja Forms JavaScript lifecycle and choosing the correct event or server-side filter, you can successfully update field values at the right moment in the submission process.
Related Support Threads Support
-
Update value on submit before submissionhttps://wordpress.org/support/topic/update-value-on-submit-before-submission/
-
Dynamic fieldhttps://wordpress.org/support/topic/dynamic-field/
-
More modern field types?https://wordpress.org/support/topic/more-modern-field-types/
-
Submitting Dynamic List Fieldshttps://wordpress.org/support/topic/submitting-dynamic-list-fields/
-
Pods Framework Custom Fieldhttps://wordpress.org/support/topic/pods-framework-custom-field/
-
listcheckbox and SQL basehttps://wordpress.org/support/topic/listcheckbox-and-sql-base/
-
Display checkbox list and file upload value in PDF submissionhttps://wordpress.org/support/topic/display-checkbox-list-and-file-upload-value-in-pdf-submission/
-
Updated list options during form render not in {all_fields_table}https://wordpress.org/support/topic/updated-list-options-during-form-render-not-in-all_fields_table/
-
Action/filter after update form settingshttps://wordpress.org/support/topic/action-filter-after-update-form-settings/
-
Select Field without First Option Selectedhttps://wordpress.org/support/topic/select-field-without-first-option-selected/
-
Querystring with number form fields not workinghttps://wordpress.org/support/topic/querystring-with-number-form-fields-not-working/
-
Dynamic value passinghttps://wordpress.org/support/topic/dynamic-value-passing-3/
-
How to autopopulate fields with querystring using list optionshttps://wordpress.org/support/topic/how-to-autopopulate-fields-with-querystring-using-number-field/
-
Custom select fieldhttps://wordpress.org/support/topic/custom-select-field/
-
Year/Make/Model fieldshttps://wordpress.org/support/topic/year-make-model-fields/
-
Dynamically change values AND lookshttps://wordpress.org/support/topic/dynamically-change-values-and-looks/
-
Select Option List Item Removinghttps://wordpress.org/support/topic/select-option-list-item-removing/
-
Select field populated with users in a specific rolehttps://wordpress.org/support/topic/select-field-populated-with-users-in-a-specific-role/
-
Single Select Dropdown Value Issuehttps://wordpress.org/support/topic/single-select-dropdown-value-issue/
-
No way to filter merge tag value if field is a listhttps://wordpress.org/support/topic/no-way-to-filter-merge-tag-value-if-field-is-a-list/
-
I need to add family Member’s Information in form’s field, Any optionhttps://wordpress.org/support/topic/i-need-to-add-family-members-information-in-forms-field-any-option/
-
Dynamic List Fields are not being submittedhttps://wordpress.org/support/topic/dynamic-list-fields-are-not-being-submitted/
-
Change Text field to dropdown using PHPhttps://wordpress.org/support/topic/change-text-field-to-dropdown-using-php/
-
Copy Fields to another formhttps://wordpress.org/support/topic/copy-fields-to-another-form/
-
hidden fieldshttps://wordpress.org/support/topic/hidden-fields-18/