How to add a custom form for a custom calculation

Hi,

I’m looking for a good way to add a form to each item of a shop system. The form takes some inputs, calculates a result and gives the user the result and some information regarding the result.

My first intention was to use the Custom Code element, creating a form with PHP and HTML, doing the calculations in JavaScript and presenting the final results (and information). But as the code is executed by the user (I don’t yet have Bricks Builder, so I can’t test it yet), this doesn’t sound like the optimal solution.

Then I stumbled onto the Form Element and its custom actions. Here I would write a custom action in PHP, but I’m not sure if I can dynamically present the result to the user, e.g. by using JS.

Can someone tell me if my above problem can be solved by either solutions, and if with the former if it is practical for the user, or if there is any other way to solve this?

Kind regards
Gerrit

javascript is way to go but need to see the form what needs to be done to see it.

It is five numbers (measurments of the body) and then I have multiple tables from which a value is calculated. Its not a simple calculation, but if I were to use Javascript I would probably write a service API and just do a simple Http request to get the result and present it.

My problem is also not the calculation, but how to best do it with Bricksbuilder. And the warning on the custom code element about enabling code execution only for users I 100% trust made me wonder, if the user can change part of the PHP code before execution (my guess would be no).

always give the code execution perm to admin only and make anyone else editor.

nobody else needs to be admin other than us.

and yes its safe.

Thank you, but that won’t help me with my problem, as I get values from multiple tables according to the input from the user.

I now had the possibility to check out bricks builder. The Form Element is not an option, as I am not able to return HTML as a response and I also don’t want to redirect to another page.

The way to go is then to use the custom code element. As I want to return the result of my calculation in place without a page reload, the way to go is with JS.

Is there any way to create an API endpoint or call a function from the functions.php from the JavaScript of the custom code element?

1 Like

It’s a shame really not being able to return custom HTML, or atleast, passing the AJAX returned data to a custom JS function.

1 Like