Forums

How to display the Sum of two Fields

ONERO 30 May, 2018
Good Day,

I have a form where a user can select a Purchase Price from a dropdown field and a Bond Price from a second dropdown field.
The related cost estimates for each are displayed when the user submits the form but I would like to show a grand total as well (Purchase Price + Bond Price = Total).
Field one is named: {purchase} and field two: {bond}.
Up to this point everything is working as it should but I lack the knowledge of the syntax to display the Grand Total.

This is what I am trying to achieve: Your Total for Registration Estimate : {purchase} + {bond} = ([purchase]+[bond]). Please excuse incorrect syntax of the last bit!
Can someone please assist or point me to a document or page that can be of assistance?

Kind regards,

Liz
GreyHead 30 May, 2018
1 Likes
Hi Liz,

Please see the PHP Code section on page 34 of the CFv6 manual.

Bob
ONERO 30 May, 2018
Dear Bob,

Thank you so much for you quick response.
I read through the page you suggested and came up with the following solution:

<?php
$purchase = $_POST['purchase'];
$bond = $_POST['bond'];
$total = $purchase + $bond;
?>
<h2 style="color:#9B1C1F;">Your Total for Registration Estimate :R {purchase} + R {bond} = <?php echo "R ". $total; ?></h2>

That looks like this on the front-end:
​[h2]Your Total for Registration Estimate :R 28648 + R 18808 = R 47456[/h2]
Now I just need to figure out how to insert decimal spaces and commas since these are currency values but I am sure I will find something on the net.

Kind regards,

Liz
GreyHead 30 May, 2018
1 Likes
Hi Liz,

I see that you are using CFv5, not v6 - sorry I didn't spot that before.

ChronoForms adds the form data to the $form->data array so you can use that in place of $_POST if you prefer.

For the formatting you should be able to use the PHP number_format() or money_format() methods.

Bob
ONERO 30 May, 2018
Dear Bob,

My mistake, I didn't mention that I was using CFv5 in my original post, so it's all good.
I just needed someone to point me in the right direction and that is what you so did. From there I was able to search online and find the correct syntax.

You are right, I found the number_format() method online and this is now what my code looks like:
<h2 style="color:#9B1C1F;">Your Registration Estimate</h2>
<p>Transfer Costs: <?php echo "R ". number_format("$purchase",2,",","."); ?><br />Bond Costs: <?php echo "R ". number_format("$bond",2,",","."); ?></p>
<p>Total: <?php echo "R ". number_format("$total",2,",","."); ?></p>

It is working perfectly and I am happy to report that I truly did learn something new today!

Thanks so much for all your advise and guiding me in the right direction.

Liz
This topic is locked and no more replies can be posted.