Retrieve Formit Forms id or hash to save in custom table

I have a formit form that uses the FormItSaveForm hook.

I have created a modx snippet that includes a PHP file that saves posted data that I need in order to render pins on a web page that displays a Google map.

Additionally, I would like to save the formit form id or hash from the record that got created in the modx_formit_forms table to my custom table.

How would I pass along the id or hash to my PHP file that gets included from a modx snippet I created.

The formit snippet call looks like this:

[[!FormIt?
&hooks=FormItSaveForm,add_location
&formName=Survey: [[!display_custom_context? &context_name=rural_location_display_name]]
&clearFieldsOnSuccess=1
&successMessage=Thank you!
&validate=nospam:blank, search:required, address_type:required
]]

The modx snippet call looks like this:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

include $_SERVER['DOCUMENT_ROOT'] . 'path/to/include/add_location.php';

I’ve included the config.inc.php, config.core.php, and the modx.class.php in my external PHP file and can run functions like writing to the error log

$modx->log(xPDO::LOG_LEVEL_ERROR,LINE);

But still cannot figure out to get the formit form id or hash from the record saved from submitting the form, short of just running a database query that gets the latest id saved to the modx_formit_forms table.

I haven’t tested this, but have you tried using

$hash = $hook->getValue('hash');
$form_array = $hook->getValue('savedForm');

in your add_location snippet.

1 Like

@halftrainedharry
Thank you!

Yes. I have tried those previously and tried again just now and receive the following Fatal Error:

Fatal error : Uncaught Error: Call to a member function getValue() on null

Using

    $modx = new modX();
    $hash = $hook->getValue('hash');

Why do you create a new modX instance in your external PHP file? Can’t you just load a class and pass $modx (and maybe $hook) as a parameter to the constructor?

1 Like

@halftrainedharry
I don’t get very far with using modx in classes. I cannot even write a log. That’s why I just use a PHP file with functions.

class FiberLocations {

	public $modx;

	function __construct(modX &$modx) {

		$this->modx =& $modx;

	}

	function handle_requests() {

		$this->modx->log(xPDO::LOG_LEVEL_ERROR, __LINE__);

	}
}

$fiber_locations = new FiberLocations();

$fiber_locations->handle_requests();

You have to pass $modx to the constructor.

$fiber_locations = new FiberLocations($modx);
1 Like

@halftrainedharry

$fiber_locations = new FiberLocations($modx);

That would do it! Thank you!

Let me see what I can do with retrieving the Formit Form ids and get back to you.

I am not able to get the hash with:

$hook->getValue(‘hash’);

But I am able to get the saved form values, which is something I can work with and am also able to get the hash by using this:

$savedForm = $this->hook->getValue('savedForm');
$this->modx->log(xPDO::LOG_LEVEL_ERROR, print_r($savedForm['hash'], true));

This will work nicely with my application.
Thank you for your help on this!