Forums

additions

berniez 19 Feb, 2011
Hi everyone, i am new to PHP and the calculations and what I am going to ask is probably simple but I am stumped. Here goes. I am using Chronoforms and Joomla and I need a very simple addition done on a couple of input fields.
field1 = no. of nights stay
field2 = no. of adults
field3 = total cost
The thing is the prices change (example)
2 adults $140 per night
3 adults $200 per night
4 adults $250 per night
up to 8 adults, but if I can get the first 3 working I can do the rest (hopefully)
I need to find out how many adults(field2) for how many nights(field1) = total payment(field3)
I am having problems with the price change.
Anyone wanting a beer please help.
Thanks, Bernie
GreyHead 19 Feb, 2011
Hi Bernie,

Please try this in the OnSubmit Before Email box (make sure that 'Send Emals' is set to 'Yes' on the form General tab so that this code is run). Ypu'll need to edit this to match the names of your inputs if they aren't field1, field2, etc.
<?php
$nights = JRequest::getInt('field1', '', 'post');
$adults = JRequest::getInt('field2', '', 'post');
$price_array = array (
  0 => 0,
  1 => 140,
  2 => 200,
  3 => 250
);
$cost = $nights x $price_array[$adults];
JRequest::setVar('field3', $cost);
?>

Bob
berniez 20 Feb, 2011
Thanks for that, I was away over the weekend and only just trying it now. I can see that is easier than I figured with the array, but being new to this i would still get lost. i have tried as you suggested but get this error:-
Parse error: syntax error, unexpected '=', expecting ')' in /home/web246/public_html/components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 5
Also, with this when the person fills in fields 1 & 2 does field3 (total price) fill in automatically before the email sends, so that the person can change their mind if the price is not right?? With field3 I also noticed that it is an input field should this not be just a textbox with the total cost or is this right? I have bought a lot of your tutorials and am reading my way through them. Keep up the great work. Thanks, Bernie
GreyHead 21 Feb, 2011
Hi Bernie,

Sorry my mistake, an array uses => not =

I've corrected my previous post.

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