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); ?>
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
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
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);
}
}
}
}
}
?>
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
Hi @mattmcca,
Glad to hear you managed to resolve it. Thanks for sharing the solution
Best Regards,
Nithin