This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 4

Use of webhooks to update another form


  1. timcov
    Member

    Is it a reasonable use of webhooks to send a submitted form's data to a custom URL on the same installation, which uses that information to update the entries of another form in the machform database?

    I have a registration form for members to register and subsequently want to automatically update a field in that table (eg last submission date) whenever a registered user completes one of my other forms. For example fill in a form to register and subsequently fill in forms to book onto events. I'd like the entries in the registration table to have fields for "last booking" and "booking count" which need to be updated each time the booking form is submitted.

    I think my limited php knowledge should manage writing a simple update query as the URL target of the webhook and think this should be far safer than trying to adapt the post-functions.php etc code to do the same thing?

    Posted 10 years ago #
  2. yuniar

    Yes, that is reasonable to do so. As a matter of fact, that's the whole purpose of the webhook. So that you won't modify the post-functions.php file directly.


    MachForm Founder

    Posted 10 years ago #
  3. williamansley
    Member

    @timcov: Did you ever get this working? If so, and you don't mind taking the time, could you provide some details on what you did? Or has anyone succeeded in using the new "webhooks" feature of Machform to use one Machform form to update another and is willing to provide some guidance? Thank you!

    Posted 10 years ago #
  4. yuniar

    It is possible to do so.

    However, you still need to do some custom coding to build the script that accept the data from the webhook and update any data within the database (or do any other custom functions).

    So, using webhooks, you won't need to modify any machform files to "send" the data to any other URL.

    Now all you need to do is to build the receiver script that accept the data. The script can be placed outside your machform folder, so that it won't get lost when you update your machform.

    I won't be able to assist with the custom receiver script though. Sorry.
    You'll need to hire a programmer for this part.


    MachForm Founder

    Posted 10 years ago #
  5. timcov
    Member

    @williamansley - Yes I did.

    I'm no programmer but managed it with a simple .php file to do the database update. The notifications section of my secondary form has "send form data to another website" enabled and the url is the url of my new php file (which happens to be in my machforms directory). I use the "Http Post" and "Key value pairs" options and in my case I have a parameter called "lastlogin" with the value {date_created}, "usercode" with the value {element_4} (which happens to be the unique key field in the master form).

    My new .php file itself contains the following:

    <?php
    	require('includes/init.php');
    	require('config.php');
    	require('includes/db-core.php');
    	$form_id = 12345;   // the master form id
    	$logindate = $_POST['lastlogin'];
    	$user = $_POST['usercode'];
    
    $dbh = mf_connect_db();
    			$query = "UPDATE ".MF_TABLE_PREFIX."form_{$form_id} SET element_7=? WHERE element_4 = ?";
    		   	$params = array($logindate,$user);
    	   		mf_do_query($query,$params,$dbh);
    
    ?>

    I don't suppose the above is how a "proper coder" would do the job and haven't even checked which of the "requires" are actually needed but it did the job for me and I'm expecting to use it a lot more extensively going forward.

    To better explain my application, I want to register new users in the primary form and record the multiple entries from existing users in a separate secondary form - but update their entry in the primary form with a "last login" date.

    In my case I therefore have another custom php file to request the user's id (the "usercode" field in my example) and check whether that exists as an entry in the primary form. They are then redirected to the primary user registration form first if it doesn't exist or into the secondary "entries" form if it does exist (with the usercode as a query parameter to pre-fill element_4 in this example). The webhook attached to the secondary entry form then updates the user's master entry in the primary form with their "last login" date (or such other data as needs to be updated) when submitted.

    Hope this helps. With webhooks it now seems so simple to link data in different forms that I'm surprised Yunair is not inclined for the core program to develop that as a feature but I understand Machform's focus is to keep it as a one time form data collection tool.

    Posted 10 years ago #
  6. williamansley
    Member

    @timcov: Thank you very much for responding. You have certainly given me a much better place to start than I had before. I will see if I can adapt the information you have given me to my situation. I may ask further questions if I get stuck, but I will understand if you don't have time to answer.

    Posted 10 years ago #

RSS feed for this topic

Reply