1

I have a ninja form that redirects to a thank you page. I am trying to process the values submitted in the form to display different content on the thank you page. I have achieved this in Ninja Forms 2 using the following code, but it doesn't work in Ninja Forms 3. And I can't work out what to do with their example code given here http://docs.ninjaforms.com/customer/portal/articles/1981022-actions-methods-processing-ninja_forms_post_process.

Here is what I have successfully used in Ninja forms 2.

$form_id ='11';
$user_ID = get_current_user_id();

$args = array(
'form_id'   => $form_id,
'user_id'   => $user_ID,

);
// This will return an array of sub objects.
$subs = Ninja_Forms()->subs()->get( $args );


    $counter=1;

foreach ( $subs as $sub ) {
            if($counter==1){

$form_id = $sub->form_id;
$user_id = $sub->user_id;
// Returns an array of [field_id] => [user_value] pairs
$all_fields = $sub->get_all_fields();

 print_r($all_fields);

echo $sub->get_field( 34 );
            } 
            $counter++;

}

1 Answer 1

1

FWIW, I use this in 3.6, and adapted from Ninja Form Codex

  1. create a js file to "listen"
  2. 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();

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.