• Resolved Mattmcca

    (@mattmcca)


    Hello I am working on a new feature for my project using the forminator, I am trying to insert a log item into a table. Upon clicking success I want to create an insert into another table.

    This is my code below, for some reason the submit success isnt working, i am using the correct form Id for it to be exclusive for one particular form.

    <?php
    /**
    *Plugin Name: Forminator Purschase Extenstion
    *
    */
    add_action( 'wp_footer', function(){
    global $wpdb;
    
    ?>
    <script type="text/javascript">
    		(function ($) {
    		$(function () {
    			$(document).on("forminator:form:submit:success", function (e, id) {
    				if(id === 2001){
    					var dataUID = $(#forminator-module-${id}).attr("data-uid");
    					var numberOfBookings = #forminator-module-${id} #forminator-field-number-1_${dataUID};
    		
    					alert(numberOfBookings)
    
    				}
    			});
    		})
    		})(window.jQuery);
    </script>
    <?php }, 21); ?>

    At the moment there is no insert that is true as i am trying to see if the function work as expected

    Could it be due to the fact that I already have another custom function in wp_footer

    • This topic was modified 2 years, 2 months ago by Mattmcca.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter Mattmcca

    (@mattmcca)

    Hello, I am just posting an updated version of my code, I have got the insert to work but not using the method forminator:form:submit:success instead it was working using the method after.load.forminator which is not what i want. I would like the insert to happen on form submit.

    <?php
    /**
    *Plugin Name: Forminator Purschase Extenstion
    *By : Matthias McCarthy
    *Description: An extenstion for Forminator to insert transactions after submitting a form
    *Version: 1.0.0
    *
    */
    add_action( 'wp_footer', function(){
    global $wpdb;
    $current_user = get_current_user_id();
    
    
    ?>
    <script type="text/javascript">
    		(function ($) {
    		$(function () {
    			$(document).on("forminator:form:submit:success", function (e, id) {
    				if(id === 2001){				
    					var link="<?php echo admin_url('admin-ajax.php')?>";
    					var dataUID = $(#forminator-module-${id}).attr("data-uid");
    					var numberOfBookings = #forminator-module-${id} #forminator-field-number-1_${dataUID};
    					const loggedInUserID = <?php echo json_encode($current_user)?>
    
    						console.log(loggedInUserID)
    						console.log($(numberOfBookings).val())
    
    						jQuery(document).ready(function($){
    							 $.ajax({
    						 	 	url: link,
    							 	contentType: "application/json; charset=utf-8",
    						 	 	data:{
    							  		action:'complete_purchase',
    							  		userId: loggedInUserID,
    							  		numberOfBookings: $(numberOfBookings).val()
    						 	 	}
    					 		 });
    						})
    	
    
    				}
    			});
    		})
    		})(window.jQuery);
    </script>
    <?php }, 21); ?>
    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @mattmcca,

    I hope you are doing well today!

    Here are the actions you can use after form submission;

    forminator_form_before_handle_submit
    forminator_form_after_handle_submit
    forminator_form_before_save_entry
    forminator_form_after_save_entry

    Kind regards,
    Zafer

    Thread Starter Mattmcca

    (@mattmcca)

    hi @wpmudevsupport15

    thanks for your reply Just want to ask where does this method go please. does it go in this section

    $(document).on("forminator:form:submit:success", function (e, id) {
    
    
    $(document).on("forminator_form_after_save_entry", function (e, id) {
    

    thanks once again for helping me

    Thread Starter Mattmcca

    (@mattmcca)

    Hello Just an update, I have converted the code to use only php removing all JavaScript , but it is still not working, any advice as to what i am doing wrong or a way to debug

    <?php
    /**
    *Plugin Name: Forminator Purschase Extenstion
    *Author : Matthias McCarthy
    *Description: An extenstion for Forminator to insert transactions after submitting a form
    *Version: 1.0.1
    * License: GPLv2 or later
    
    *
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    // No need to do anything if the request is via WP-CLI.
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'Forminator_Insert_Credit' ) ) {
    	class Forminator_Insert_Credit {
    		private static $_instance = null;
    
    		public static function get_instance() {
    
    			if( is_null( self::$_instance ) ){
    				self::$_instance = new Forminator_Insert_Credit();
    			}
    
    			return self::$_instance;
                
    		}
    
    		public function __construct() {
                
    			$this->init();
    			// Ajax method (action)
    			add_action( 'forminator_form_after_handle_submit', 'insertIntoCreditLog', 10, 2);
    			add_action('forminator_form_after_save_entry', 'insertIntoCreditLog', 10,2);
    			add_filter( 'forminator_form_ajax_submit_response','insertIntoCreditLog', 10,2);
    			// Post method (filter)
    			add_filter( 'forminator_form_submit_response', 'insertIntoCreditLog', 10,2);
    
    		}
    
    		public function insertIntoCreditLog($form_id, $response) {	
    			if ($response['success']){
    				if ($form_id == MyFormID){
    					$numberofbookings = $_post['number-1'];
    					$current_user = get_current_user_id();
    						//create insert into php here 
    							global $wpdb;
    							$table ='insert_Table_Name';
    							$data = array (
    								'credits' => -$numberofbookings,
    								'action' => 'transaction',
    								'user_id' => $current_user
    							);
    
    						$wpdb->insert($table,$data);
    					}
    			}
    		}
    	
    	}
    	
    }
    ?>
    Thread Starter Mattmcca

    (@mattmcca)

    Hi All, I managed to solve this issue myself, after a lot of trial and error. As a way of helping others that may still be new I have upload my code to github for all to see. I believe in karma do good and good comes your way

    https://github.com/mccamatt92/Forminator-insert-into-table/blob/main/forminator-insert-into-database.php

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @mattmcca,

    Glad to hear you managed to resolve it. Thanks for sharing the solution

    Best Regards,

    Nithin

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Submit Success Not Working’ is closed to new replies.