FWIW, I use this in 3.6, and adapted from Ninja Form Codex
- create a js file to "listen"
- create a php script to process
/**
* File name: processNinjaformOnSubmit.js
* Place in <themename>/assets/js
* Desc: processes form data upon submit by "listening" for submit
* and then processing Ninja form
*/
var mySubmitController = Marionette.Object.extend( {
initialize: function() {
this.listenTo( Backbone.Radio.channel( 'forms' ), 'submit:response', this.actionSubmit );
},
actionSubmit: function( response ) {
//get the form id, e.g., nf-form-1-cont
processForm( jQuery('.nf-form-cont').attr('id') );
},
});
// On Document Ready…
jQuery( document ).ready( function( $ ) {
// Instantiate our custom field's controller, defined above.
new mySubmitController();
});
function processForm( form_id) { //form_id is passed by Marionette in the footer.php
let result = form_id.match(/nf-form-(\d+)/);
form_number = result[1];
//get all the field names
let fields;
jQuery('#'+form_id+' form').each(function(){
fields = jQuery(this).find(':input')
});
//build a name:value hash
let nvhash = {};
nvhash[ "form_number"] = form_number;
//populate the hash of name/values from all the input fields
for (i = 0; i < fields.length; i++) {
//get values of all checkable fields
if ( ( fields[i].type == 'checkbox' || fields[i].type == 'radio' ) && jQuery('#'+fields[i].id).is(':checked') ) {
nvhash[ fields[i].id ] = fields[i].value;
//get values of all non-checkable fields
} else if ( fields[i].type != 'checkbox' && fields[i].type != 'radio' ) {
nvhash[ fields[i].id ] = fields[i].value;
}
}
//use this data to populate a result page or record to database:
jQuery.ajax({ //call a PHP script to process the form
type: 'POST',
url: 'https://somedomainname.com/wp-content/themes/themename/processNinja.php',
dataType: 'json',
data: { nvhash:nvhash },
success: function(response) {
console.log(response); //response is the returned result
}
});
return false;
}
PHP:
<?php
/* Do something with Ninja form data */
/* Place in <themename>/ and call processNinja.php */
/* Fired by ajax call in processNinjaformOnSubmit.js */
/* In this example:
* 1. the data hash is retrieved from the ajax call
* 2. the field names for the form are retrieved from the Ninja table in the DB
* 3. a associative array is built
* 4. the data is inserted in a bestoke table of your making
*/
//calling a .php file via ajax requires we recall the DBI
$root_dir = '/usr/home/myhome/public_html/somedomainname/';
require_once $root_dir."wp-load.php";
global $wpdb;
$form_data = $_POST['nvhash']; //get the name-value pairs from the calling ajax
$form_number = $form_data['form_number']; //retrieve one variable
//get the ids of the fields
$field_ids = array();
foreach ($form_data as $key => $value) {
preg_match('/nf-field-(\d+)/', $key, $matches);
array_push($field_ids, $matches[1]);
}
//get column names
$table_meta = $wpdb->get_results(
$wpdb->prepare( 'SELECT id AS fid, type, REGEXP_REPLACE(field_key, "_[0-9]+$","") AS column_name
FROM wp_nf3_fields
WHERE field_key NOT REGEXP "submit" AND parent_id = %d', $form_number )
);
$name_values = array();
$placeholders = array();
//build the column/values asso array for database query
foreach ( $field_ids as $fid ) {
foreach ( $table_meta as $key ) {
if ( !empty( $form_data['nf-field-'.$fid] ) && $fid == $key->fid ) { //if there is data
$name_values[$key->column_name] = $form_data['nf-field-'.$fid]; //build array
array_push( $placeholders, '%s' );
}
}
}
// make the query
$inserted = $wpdb->insert(
'wp_contact_me', //your bestoke table name
$name_values,
$placeholders
);
$wpdb->flush();
?>