Custom entries to custom wp table
-
Hello!
I’m trying to set up in a new account creation form that if somebody put a custom text/number to a field name text-2, forminator should put this date to a unique table.
Here is my code – but not working. Can you help to correct – what am I using wrong (the table name start with wp_ is correct: wp_prflxtrflds_user_field_data, fix field_id value is 10, so I don’t understand that after the new user created, the user_value why not there.
Code:
https://pastebin.com/4gCxnd0A
Thank you,
PeterThe page I need help with: [log in to see the link]
-
Hope you are doing fine!
To determine why the value is not saved, we’ll need to perform a few tests on our end. Could you help us sharing the form export which triggers the code snippet provided above, so that we could review the existing settings?
Please take a look at the following doc on how to export a form:
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-exportIf you are concerned about any sensitive information in the form, then you can duplicate your form, remove any sensitive information, and then export it.
You can share the export file via Google Drive, Dropbox or any cloud services in the next reply.
Looking forward to your response.
Kind regards
Luis
Hello Luis!
This is the export:
https://pastebin.com/t1j0xFnj
Thank you,
PeterSorry, I put a wrong form 🙁
The good code with the registration form is this:
https://pastebin.com/XjZvVKeW
(when I check “existing customer” – yes”, then a field coming (text-2 – proximity card id)
But you think that this snippet is okay what I sent to you? I mean, every phrase or syntax is correct?
Thanks,
PeterThanks for response!
The code snippet is not okay, actually. It’s not even fired up (so callback function is not executed either) because there is no forminator_custom_form_process action hook in Forminator.
Since you are targeting registration form, you can use forminator_cform_user_registered instead. Here’s a modified code:
add_action( 'forminator_cform_user_registered', 'wpmu_user_registered_custom_code', 11, 3 ); function wpmu_user_registered_custom_code( $user_id, $form_id, $data ) { global $wpdb; $user_value = $data->meta_data['text-2']['value']; $wpdb->insert( $wpdb->prefix . 'prflxtrflds_user_field_data', array( 'user_id' => $user_id, 'field_id' => 10, 'user_value' => $user_value ), array( '%d', '%d', '%s' ) ); }
Note: I did not test if it correctly saves the data as I don’t have such DB tables and am not sure what’s the correct structure – but I can confirm for sure that the code at least gets executed and has correct user ID and text-2 field value. I think it should also be saving it now if the table exists and has correct name.
Best regards,
AdamThank you, Adam, perfectly working.
Just a question!
If I’m creating a new form, but not a user-registration type, a normal type, because it is easier to update these table datas with a form, like every time going to the database and update the tables. So if I’m creating a form what I want to using just for updating multiple users data (5 values, Proxy ID, Access Control ID and maybe I’m inserting more lines, which action should I call? Is there any option in forminator then If this is not a registraton form, after submit the code inspect the existing date, if exist, update, if not, insert? (if, else – I will create the snippet, just please help me that forminator can do this, or only from registration form?
Or the better is, 1. I create the form, 2. I create the snippet, and IF not working, I’m opening a new topic or not closing this? I will do this tonight.
So the action name is the important what can update and insert – I think not this “cform_user_registerd”
Thank you,
Peter
UPDATE: Of course this form only for admins, so I not put this to the front, so if I’m, logging with specific user id’s or administrator role can use this. But I don’t know forminator can use roles, so if I’m putting to the code user role admin it will be help to update a different users, or only during registration can do this with the specific?- This reply was modified 1 year, 5 months ago by hollosipeter. Reason: UPDATE
I’m glad it works for you!
As for your questions, let me try to address them in points for clarity 🙂
1. the “forminator_cform_user_registered” action hook is only for registration forms and it will not work with other types of forms; I suggested it because, based on the code that you shared, I assumed it is supposed to be for registration forms only.
2. it is possible to do similar thing with regular (non-registration) forms but you will need to use different hook and the code will need to be slightly modified.
Here is example (see code comments, they explain everything):
// first, let's get Submission ID and save it in temporary $_POST key // otherwise it will not be available later add_action( 'forminator_custom_form_submit_before_set_fields', 'forminator_custominsert_entry_id', 20, 3 ); function forminator_custominsert_entry_id( $entry, $form_id, $field_data_array ) { $_POST['_forminator_cform_entry_id'] = $entry->entry_id; } // now the actual code to be run on ANY form submission add_action( 'forminator_form_after_save_entry', 'forminator_custominsert_entryid_set', 10, 2 ); function forminator_custominsert_entryid_set( $form_id, $response ) { global $wpdb; // get entry ID $entry_id = $_POST['_forminator_cform_entry_id']; // fetch entry data $data = Forminator_API::get_entry( $form_id, $entry_id ); // and let's get our field from data $user_value = $data->meta_data['text-2']['value']; /* USER ID - this is different because that's not a registration form so it is not available in form. There are two things that could be done here to get it 1. you can add hidden field to the form (e.g. {hidden-1}), set to include user ID and then fetch user ID like this right frm the field $user_id = $data->meta_data['hidden-1']['value']; 2. or you can use some "global" WP way to get id of a currently logged-in user, similar to this $user_id = get_current_user_id(); */ // and the rest is the same as in registration form code $wpdb->insert( $wpdb->prefix . 'prflxtrflds_user_field_data', array( 'user_id' => $user_id, 'field_id' => 10, 'user_value' => $user_value ), array( '%d', '%d', '%s' ) ); }
3. I’m not sure what exactly do you mean by updating “multiple users” in this case but
– if it means code is run for multiple users but “one user at the time” or rather “when user submits the form” – above should be fine
– if one submission of the form is supposed to update multiple user data then you will need to find the way to pass all the user IDs to the code and you’ll need to modify the code that inserts to DB, for example wrap it in some loop
4. Forminator doesn’t support roles if I correctly understand what you mean. But it can be set to show form only to logged-in users. If the code is to be run only for certain roles then you need to extend it – to check user role after fetching user ID and then use “if” condition.
I hope this helps.
Please not that any further modification or extension of those shared codes are way beyond the scope of this support so we won’t be able to provide complete working code for that. But I hope this would give you a good foundation to work with.
Best regards,
AdamThank you Adam, and of course I’m always trying make the codes myself or searching in the github and if something is not clear I contacted you. Thank you for the help in this and the other topic too. ☺️
Best regards.
PeterHello Adam!
I made a code upon your last answer, it is perfectly working, thank you.
But I just realized that these codes is only working in WordPress environment. Because my developer wanted to from another system with a POST method using the form. The form is submitted, but the database updates isn’t happened. Can maybe I add the code to the form directly, because i want to use manually and want to use with a data synchron button from another user db?Really thanks,
PeterYes, those codes do work in WordPress only as we “by default” assume it’s for the site where Forminator is installed – it’s 99,9% of use cases and Forminator itself is a WordPress plugin 🙂
I suppose it may be possible to modify that slightly but I’m not sure if I correctly understand so let me ask first which scenario do you mean:
1.
– the form is on Site A
– it’s submitted on Site A
– but the DB update part actually updates data in DB on Site B2.
– the form is on Site A
– and DB update part is updating DB data on Site A
– but the form itself is submitted/gets submission data from Site B?That second case – I mean Site B sends all the data to the Form on Site A via POST request…
Or is it yet something slightly different?
Best regards,
AdamHello Adam!
The last option:
“That second case – I mean Site B sends all the data to the Form on Site A via POST request…”another system try to call the form in a window and submit – but the wordpress code is not refreshing the db
thank you:
PeterThanks. I’m still not sure how the form is called then – it would be important to know if it’s some kind of a non-standard solution. It may indeed not work in this case.
In general, if you need to submit data to the form “from outside”, this should be done using the “pre-populate” form feature and if set correctly, all should work out of the box, just like it was all happening “locally”:
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#pre-populate-form-field-values
Otherwise it may not be working correctly, I’m afraid. But to tell more we would need to know exactly how this form is “called”/used (including code – though without any “sensitive data” of course – that does that).
Kind regards,
AdamData comes from a button from an outside system:
The developer put button to the outside system and try to fetch date from here, opening the new wordpress window.
A) after the wordpress/forminator window is opened, all requested datas filled, but I have to push manually the “submit” button. In this case wordpress code for database insert/update is working.
This is: https://pastebin.com/dPYDaPsn
B) after the wordpress/forminator window is opened, the form automatically submitted and datas are correct if I check in submissions, but the code not run and making insert/update. In this scenario developer said he have to fill some plus hidden fields because Forminator asks. Maybe he is wrong.
This is: https://pastebin.com/pmVaWwN2
The php code what I’m using and created upon your instructions above (thanks again):
https://pastebin.com/k4q2Fca7
This code I like to be work from “outside” (another exciting thing that forminator can be automatically close the window after I’m getting back the behavio/green indicator with the submission is okay?)
But of course first I have to know that why not running the code:
And this is the forminator form (very simple and short, no roles, optional field with few update-able data:
https://pastebin.com/TqDwqc9j
What you wrote “if you need to submit data to the form “from outside”, this should be done using the “pre-populate” form feature and if set correctly, all should work out of the box, just like it was all happening “locally” – how can I do? Because I see your spec, there is 2 forminator forms update-ing each other (1 to 2) but my scenario datas come not from the forminator.
Really thank you,
Peter- This reply was modified 1 year, 5 months ago by hollosipeter.
- This reply was modified 1 year, 5 months ago by hollosipeter.
- This reply was modified 1 year, 5 months ago by hollosipeter.
(sorry, reverse: if we put the hidden input fields, in this case form opening and I can submit manually, but if not, form autosubmitted, submission is okay, but code is not insert/update). Thanks: Peter
So if I correctly understand what the developer did is this:
– they, sort of, replicated form fields on their end; so that’s a regular form but just “invisible”
– and the form URL that they set is the URL of your WP page where the actual Forminator form is locatedInstead of that, try much simpler solution:
1. first, configure pre-populate for your form fields on your form – just like explained in the docs (forget for a second about “other forms”.
Basically, you just need to do this:
a) for fields other than “hidden” type, simply put some unique URL parameter name in “Pre-populate” option in settings; example:
b) for field of type “hidden”, select “Query Paremter” option from “Default value (optional)” drop-down and put some unique URL parameter name in “Query parameter” field; example:
Once you got that set, update the form.
Now you can test it easily right in the browser. Let’s stick to above two example fields and let’s say that your Forminator form is on “yoursite.com/contact” page:
https://yoursite.com/contact/?myname=Adam&secret_key=123
So if I now visit this URL in browser, I’ll see Formiantor form and it will already be filled-in with that data: the “Name” field will have value of “Adam” and the hidden field will have value of 123. If I submit that form, it will work just like it should (and the code shared earlier should would work just fine).
Now your developer does not have to replicate all the fields and form code. Instead they just need to make sure that button URL – the button that they generate to open the form window – uses above URL as a link and that’s it (they can add “_blank” attribute to A tag of course to open it in new tab).
Best regards,
Adam- This reply was modified 1 year, 5 months ago by Adam - WPMU DEV Support.
Hello Adam!
Thank you, I sent a screen video to wpmudev email replied to “Amaka’ from @hollosipeter.hu
After we can sure 100%, that talking the same.
This video you see the manuality, but I like if I push the button and form filling in the background (if option for this: without opening forminator in my wordpress page, if no option for this, opening the window, automatically submit, and after submit automatically close. That was the problem, because the mu-plugin not worked, just in this manual version.
So what I sent you now by e-mail can be fixed with the method what you write few minutes here?
Really thanks,
Peter
- The topic ‘Custom entries to custom wp table’ is closed to new replies.