0

So i created a custom plugin that submits an inline html form through gravity forms api. Everything works but for some reason the form is submitting twice and therefore i am getting duplicate entries. I don't know if the function is firing twice because of a redirect, or the init action is calling the submitForm(); function more than one time? I need some clarification as i know this is a simple fix and i am banging my head off the wall with this.

class gravityFormsInlineForms {

    public function submitForm() { 

        // set a hidden field named 'gf_id' on the inline form thats value is equal to the ID of the Gravity Form 

            // Run buildFormData function 
            $this->buildFormData($this->formId);

            // GFCommon::log_debug( 'submitForm(): formData => ' . print_r( $formData, 1 ) );

            // Submit formData to form
            $this->submit = GFAPI::submit_form($this->formId, $this->formData);

            // If is valid redirect
            if($this->submit['is_valid']) {

                if($this->confirtmation['type'] == "message") {

                    add_action('wp_footer', array($this, 'confirmationMessage'), 50);

                }

            }

        // GFCommon::log_debug( 'submitForm(): response => ' . print_r( $submit, 1 ) );
    }

    public function buildFormData($formId) {

        // field names on inline form always have to mirror the Gravity Form field ID

        // Retrieve form data by ID
        $gForm = GFAPI::get_form( $formId );

        // Retrieve raw data from $_GET global
        $rForm = $_POST;


        $this->confirtmation = current($gForm['confirmations']);

        // Set values to fields if $gForm = $rForm
        foreach ($rForm as $rKey => $rFormField) {

                $rData[$rKey] = $rFormField;
        }

        // Set gravity form fields and clean up
        foreach($gForm['fields'] as $gkey => $gFormField) {

            if(array_key_exists(str_replace(' ', '', $gFormField['label']), $rData)) {

                $this->formData['input_'.$gFormField['id']] = $rData[str_replace(' ', '',$gFormField['label'])];

            }   

        }

    }

    public function confirmationMessage() {

        $confirmationMessage = $this->submit['confirmation_message'];

        ?>

        <style>.gform_confirmation_wrapper {margin: 30px 0; background-color: #dff0d8; border-color: #d0e9c6; color: #3c763d; padding: .75rem 1.25rem; border: 1px solid transparent; border-radius: .25rem; }</style>
        <script type="text/javascript">jQuery(".form-body").before("<?php echo $confirmationMessage ?>");</script>

        <?php 

    }


    function __construct() {

        $this->formId = isset( $_POST['gf_id'] ) ? absint( $_POST['gf_id'] ) : 0; 

        if($this->formId) {

            add_action( 'init', array($this, 'submitForm'));

        }

    }

}

$gravityFormsInlineForms = new gravityFormsInlineForms();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.