Buy Now
Sign in

CF processing this onsubmit php array and passing variable

HSN , October 08 2012, 22:25
H
HSN 74
October 08 2012, 22:25 #300793
Hi Max & Bob,

Sorry for Duplicate not sure the best place to post now lol

Its been a while guys and I see Chronoforms has moved into V4 in this time

Anyway, straight down to business Im afraid lol, hope you can help (you helped me with this code for V3 originally Bob).

Ok so what ive done is made a new multipage form based on Bobs FAQ guide and under events in the OnSubmit section ive included a Custom Code Action.
Basically I have a list of 7 drop down elements. Each one has a number of entries with this code in the onsubmit event

<?
$beds1Tran = array(
'0 Bedrooms' => 0,
'1 Bedroom' => 0.5,
'2 Bedrooms' => 1.0,
'3 Bedrooms' => 1.5,
'4 Bedrooms' => 2.0,
'5 Bedrooms' => 2.5,
'6 Bedrooms' => 3.0
);
$beds1 = JRequest :: getString ( 'Beds', 1);
if (array_key_exists($beds1, $beds1Tran)) {
  $beds1Value = $beds1Tran[$beds1];
} else {
  $beds1Value = 0;
}

$sitting2Tran = array(
'0 Sitting or Dining Rooms' => 0,
'1 Sitting or Dining Room' => 0.5,
'2 Sitting or Dining Rooms' => 1.0,
'3 Sitting or Dining Rooms' => 1.5,
'4 Sitting or Dining Rooms' => 2.0,
'5 Sitting or Dining Rooms' => 2.5,
'6 Sitting or Dining Rooms' => 3.0
);
$sitting2 = JRequest :: getString ( 'Sitting', 1);
if (array_key_exists($sitting2, $sitting2Tran)) {
  $sitting2Value = $sitting2Tran[$sitting2];
} else {
  $sitting2Value = 0;
}

$baths3Tran = array(
'0 Bathrooms' => 0,
'1 Bathroom' => 0.5,
'2 Bathrooms' => 1.0,
'3 Bathrooms' => 1.5,
'4 Bathrooms' => 2.0,
'5 Bathrooms' => 2.5,
'6 Bathrooms' => 3.0
);
$baths3 = JRequest :: getString ( 'Baths', 1);
if (array_key_exists($baths3, $baths3Tran)) {
  $baths3Value = $baths3Tran[$baths3];
} else {
  $baths3Value = 0;
}

$kitchens4Tran = array(
'0 Kitchens' => 0,
'1 Kitchen' => 0.5,
'2 Kitchens' => 1.0,
'3 Kitchens' => 1.5,
'4 Kitchens' => 2.0,
'5 Kitchens' => 2.5,
'6 Kitchens' => 3.0
);
$kitchens4 = JRequest :: getString ( 'Kitchens', 1);
if (array_key_exists($kitchens4, $kitchens4Tran)) {
  $kitchens4Value = $kitchens4Tran[$kitchens4];
} else {
  $kitchens4Value = 0;
}

$floors5Tran = array(
'0 Floors' => 0,
'1 Floor' => 0.25,
'2 Floors' => 0.5,
'3 Floors' => 0.75,
'4 Floors' => 1.0,
'5 Floors' => 1.25,
'6 Floors' => 1.5
);
$floors5 = JRequest :: getString ( 'Floors', 1);
if (array_key_exists($floors5, $floors5Tran)) {
  $floors5Value = $floors5Tran[$floors5];
} else {
  $floors5Value = 0;
}

$othrooms6Tran = array(
'0 Other Rooms' => 0,
'1 Other Room' => 0.5,
'2 Other Rooms' => 1.0,
'3 Other Rooms' => 1.5,
'4 Other Rooms' => 2.0,
'5 Other Rooms' => 2.5,
'6 Other Rooms' => 3.0
);
$othrooms6 = JRequest :: getString ( 'OthRooms', 1);
if (array_key_exists($othrooms6, $othrooms6Tran)) {
  $othrooms6Value = $othrooms6Tran[$othrooms6];
} else {
  $othrooms6Value = 0;
}

$roomsize7Tran = array(
0 => 0,
'Small' => 1.0,
'Medium' => 1.0,
'Large' => 1.5,
'Very Large' => 2.0
);
$roomsize7 = JRequest :: getString ( 'RoomSize', 1);
if (array_key_exists($roomsize7, $roomsize7Tran)) {
  $roomsize7Value = $roomsize7Tran[$roomsize7];
} else {
  $roomsize7Value = 0;
}


$var = ($beds1Value + $sitting2Value + $baths3Value + $kitchens4Value + $floors5Value + $othrooms6Value) * $roomsize7Value * 9;

$var = round($var);

$priceone = $var - 8;

$pricetwo = $var + 2;

$session =& JFactory::getSession();
$tmp = $session->get('chrono_formpages_data_quotegen', array(), md5('chrono'));
$tmp['estimateone'] = $priceone;
$tmp['estimatetwo'] = $pricetwo;
$session->set('chrono_formpages_data_quotegen', $tmp, md5('chrono'));

?>

Then after the page break ive included a Custom Element and inserted the following code int he code box
<div class="form_item">
<div class="form_element cf_text"> <span class="cf_text">Based on your chosen criteria, <strong>your cleaning quote guide price is between: £<?

$session =& JFactory::getSession();
$tmp = $session->get('chrono_formpages_data_quotegen', array('estimateone' => 'unknown'), md5('chrono'));
echo $tmp['estimateone'];


?> & £<?

$session =& JFactory::getSession();
$tmp = $session->get('chrono_formpages_data_quotegen', array('estimatetwo' => 'unknown'), md5('chrono'));
echo $tmp['estimatetwo'];


?> </strong></span> </div>
<div class="cfclear">&nbsp;</div>
</div>


The problem I am currently experiencing is that im getting an output of £unknown & £unknown

in place of an actual figure as you would have thought with the last bit of code above.

Any ideas at all on what may be going on ?

Many thanks in advance (PS Max i replied to your email but im still unable to reset my password for the actual website )
GreyHead 64
October 09 2012, 07:52 #300803
Hi HSN,

The code looks OK, I don't see any obvious reason why $tmp isn't being passed through the session. Next step would be to add debug code and go typo hunting.

That said, ChronoForms can do this for you. Add the data to the $form->data array
$form->data['estimateone'] = $priceone;
$form->data['estimatetwo'] = $pricetwo;
and use the Multi Page action from the Utilities action group to handle the data transfer.

Please see this FAQ for more info.

Bob

PS PM or email me with a new password and I'll change it for you. I think that now has to be done from the Joomla! admin as the security has been tightened up to slow the spammers down.
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 09 2012, 08:38 #300810
The code is mostly by your instruction and design Thats why it looks so good lol.

But think thats what I may be doing wrong? I have a multipage event in the onsubmit area of the form.
GreyHead 64
October 09 2012, 09:12 #300813
Hi HSN,

If you use the Multipage action it needs to be at the top of each page event.

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 09 2012, 09:15 #300815
Hi Bob,

so
"GreyHead":
Hi HSN,

The code looks OK, I don't see any obvious reason why $tmp isn't being passed through the session. Next step would be to add debug code and go typo hunting.

That said, ChronoForms can do this for you. Add the data to the $form->data array
$form->data['estimateone'] = $priceone;
$form->data['estimatetwo'] = $pricetwo;
and use the Multi Page action from the Utilities action group to handle the data transfer.

Please see this FAQ for more info.

Bob

PS PM or email me with a new password and I'll change it for you. I think that now has to be done from the Joomla! admin as the security has been tightened up to slow the spammers down.


Which bit of code do I replace with your edits above and where ? and please call me tom (not sure why my sign aint working no more).
GreyHead 64
October 09 2012, 09:37 #300818
Hi Tom,

At the end of your code were two similar lines starting $tmp =

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 09 2012, 09:45 #300819
Hi Bob,

Ah ok so instead of the last bit in my onsubmit php code being

$var = round($var);

$priceone = $var - 8;

$pricetwo = $var + 2;

$session =& JFactory::getSession();
$tmp = $session->get('chrono_formpages_data_quotegen', array(), md5('chrono'));
$tmp['estimateone'] = $priceone;
$tmp['estimatetwo'] = $pricetwo;
$session->set('chrono_formpages_data_quotegen', $tmp, md5('chrono'));

?>


We instead want it to read this ?
$var = round($var);

$priceone = $var - 8;

$pricetwo = $var + 2;

$session =& JFactory::getSession();
$tmp = $session->get('chrono_formpages_data_quotegen', array(), md5('chrono'));
$form->data['estimateone'] = $priceone;
$form->data['estimateone'] = $pricetwo;
$session->set('chrono_formpages_data_quotegen', $tmp, md5('chrono'));

?>


You know I swear down we did say at one point the long term solution would be better to build this using a DB lol.

Thanks in advance again

Tom.
GreyHead 64
October 09 2012, 10:03 #300821
Hi Tom,

Not quite, it depends on what you already have. This should work to only update if there is a new value.
if ( $priceone ) {
$form->data['estimateone'] = $priceone;
}
if ( $pricetwo ) {
$form->data['estimateone'] = $pricetwo;
}

If you use the Multipage action you don't need to do anything directly with the session data.

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 09 2012, 10:13 #300823
Hi Bob,

Youve lost me completely now sorry :s

The code I was using (other than the html wizard generated table) was all included in my first post.

Im not sure which bit I am now to edit or alter to be honest. What I would prefer to do though is to construct the multipage form properly in the interests of it being done right, so if I include the multipage element in the onsubmit event at the top then this is what I shall have to do.

But I have to admit I do not know which bit of code to edit now, as I am not exactly php savvy as it were.

Many thanks,

Tom.
GreyHead 64
October 09 2012, 13:46 #300838
Hi Tom,

Here's a reworked version of the whole code block
<?php
$var = 0;
$beds_array = array(
'0 Bedrooms' => 0,
'1 Bedroom' => 0.5,
'2 Bedrooms' => 1.0,
'3 Bedrooms' => 1.5,
'4 Bedrooms' => 2.0,
'5 Bedrooms' => 2.5,
'6 Bedrooms' => 3.0
);

$sitting_array = array(
'0 Sitting or Dining Rooms' => 0,
'1 Sitting or Dining Room' => 0.5,
'2 Sitting or Dining Rooms' => 1.0,
'3 Sitting or Dining Rooms' => 1.5,
'4 Sitting or Dining Rooms' => 2.0,
'5 Sitting or Dining Rooms' => 2.5,
'6 Sitting or Dining Rooms' => 3.0
);
$baths_array = array(
'0 Bathrooms' => 0,
'1 Bathroom' => 0.5,
'2 Bathrooms' => 1.0,
'3 Bathrooms' => 1.5,
'4 Bathrooms' => 2.0,
'5 Bathrooms' => 2.5,
'6 Bathrooms' => 3.0
);
$kitchens_array = array(
'0 Kitchens' => 0,
'1 Kitchen' => 0.5,
'2 Kitchens' => 1.0,
'3 Kitchens' => 1.5,
'4 Kitchens' => 2.0,
'5 Kitchens' => 2.5,
'6 Kitchens' => 3.0
);
$floors_array = array(
'0 Floors' => 0,
'1 Floor' => 0.25,
'2 Floors' => 0.5,
'3 Floors' => 0.75,
'4 Floors' => 1.0,
'5 Floors' => 1.25,
'6 Floors' => 1.5
);
$othrooms_array = array(
'0 Other Rooms' => 0,
'1 Other Room' => 0.5,
'2 Other Rooms' => 1.0,
'3 Other Rooms' => 1.5,
'4 Other Rooms' => 2.0,
'5 Other Rooms' => 2.5,
'6 Other Rooms' => 3.0
);

$data_array = array(
'Beds' => 'beds_array',
'Sitting' => 'sitting_array',
'Baths' => 'baths_array',
'Kitchens' => 'kitchens_array',
'Floors' => 'floors_array',
'OthRooms' => 'othrooms_array'
);
foreach ( $data_array as $k => $v ) {
if ( isset($form->data[$k]) && array_key_exists( $form->data[$k], $$v ) ) {
$var += $$v[$form->data[$k]];
}
}

$roomsize_array = array(
0 => 0,
'Small' => 1.0,
'Medium' => 1.0,
'Large' => 1.5,
'Very Large' => 2.0
);
$roomsize7 = JRequest :: getString ( 'RoomSize', 1 );
if ( isset($form->data['RoomSize']) && array_key_exists( $form->data['RoomSize'], $roomsize_array ) ) {
$var = $var * $roomsize_array[$form->data['RoomSize']];
}
$var = round( $var * 9 );
$form->data['estimateone'] = $var - 8;
$form->data['estimatetwo'] = $var + 2;
?>

It's mostly put the repeated code into a single foreach loop which is simpler than it first looks.

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 09 2012, 14:43 #300846
Hi Bob,

Just got back in and reading over your post. Thank you for the edits. Im not sure I completely understand it all as yet though.
"GreyHead":
Hi Tom,

Here's a reworked version of the whole code block
<?php
$var = 0;
$beds_array = array(
'0 Bedrooms' => 0,
'1 Bedroom' => 0.5,
'2 Bedrooms' => 1.0,
'3 Bedrooms' => 1.5,
'4 Bedrooms' => 2.0,
'5 Bedrooms' => 2.5,
'6 Bedrooms' => 3.0
);

$sitting_array = array(
'0 Sitting or Dining Rooms' => 0,
'1 Sitting or Dining Room' => 0.5,
'2 Sitting or Dining Rooms' => 1.0,
'3 Sitting or Dining Rooms' => 1.5,
'4 Sitting or Dining Rooms' => 2.0,
'5 Sitting or Dining Rooms' => 2.5,
'6 Sitting or Dining Rooms' => 3.0
);
$baths_array = array(
'0 Bathrooms' => 0,
'1 Bathroom' => 0.5,
'2 Bathrooms' => 1.0,
'3 Bathrooms' => 1.5,
'4 Bathrooms' => 2.0,
'5 Bathrooms' => 2.5,
'6 Bathrooms' => 3.0
);
$kitchens_array = array(
'0 Kitchens' => 0,
'1 Kitchen' => 0.5,
'2 Kitchens' => 1.0,
'3 Kitchens' => 1.5,
'4 Kitchens' => 2.0,
'5 Kitchens' => 2.5,
'6 Kitchens' => 3.0
);
$floors_array = array(
'0 Floors' => 0,
'1 Floor' => 0.25,
'2 Floors' => 0.5,
'3 Floors' => 0.75,
'4 Floors' => 1.0,
'5 Floors' => 1.25,
'6 Floors' => 1.5
);
$othrooms_array = array(
'0 Other Rooms' => 0,
'1 Other Room' => 0.5,
'2 Other Rooms' => 1.0,
'3 Other Rooms' => 1.5,
'4 Other Rooms' => 2.0,
'5 Other Rooms' => 2.5,
'6 Other Rooms' => 3.0
);

$data_array = array(
'Beds' => 'beds_array',
'Sitting' => 'sitting_array',
'Baths' => 'baths_array',
'Kitchens' => 'kitchens_array',
'Floors' => 'floors_array',
'OthRooms' => 'othrooms_array'
);
foreach ( $data_array as $k => $v ) {
if ( isset($form->data[$k]) && array_key_exists( $form->data[$k], $$v ) ) {
$var += $$v[$form->data[$k]];
}
}

$roomsize_array = array(
0 => 0,
'Small' => 1.0,
'Medium' => 1.0,
'Large' => 1.5,
'Very Large' => 2.0
);
$roomsize7 = JRequest :: getString ( 'RoomSize', 1 );
if ( isset($form->data['RoomSize']) && array_key_exists( $form->data['RoomSize'], $roomsize_array ) ) {
$var = $var * $roomsize_array[$form->data['RoomSize']];
}
$var = round( $var * 9 );
$form->data['estimateone'] = $var - 8;
$form->data['estimatetwo'] = $var + 2;
?>

It's mostly put the repeated code into a single foreach loop which is simpler than it first looks.

Bob


How does this play with the second bit of code which I use on page 2 to echo the results and variables back onto the page ?
Can I safely leave these as they are or would these need altering also to include the
$form->data['estimateone'] etc 


Also second question I have is (and I was meant to ask you this a couple of years ago when this idea was first conceived) could I then also take this bit of code here
{
$var = $var * $roomsize_array[$form->data['RoomSize']];
}
$var = round( $var * 9 );
$form->data['estimateone'] = $var - 8;
$form->data['estimatetwo'] = $var + 2;


And edit/expand on this to use the same sets of figures (before the * 9 multiplication) in order to give another output eg

{
$vartwo = $vartwo * $roomsize_array[$form->data['RoomSize']];
}
$vartwo = round( $vartwo * 1 );
$form->data['estimateone'] = $vartwo - 8;
$form->data['estimatetwo'] = $vartwo + 2;


Obviously im guessing at the beginning of the form we would need to add
$vartwo = 0;


Also im thinking that
$vartwo = round( $vartwo * 1 );
is not required as its multiplying it by 1 instead of a meaningful amount so this line could be left out entirely ? if not edited to
$vartwo = round;


Please let me know if im talking dribble lol. A million thoughts going through my mind at the moment and not many of them are of much sense right now.


Kindest regards,

Tom.
H
HSN 74
October 10 2012, 11:50 #300882
Hi Bob,

Ive gona ahead an inserted the new code block you quoted above into the onsubmit event php box.

However (as I feared) it would seem that something needs to be changed on page 2 from my original code in order to get the output. :/

Kindest regards,

Tom.

PS I didnt spott he beer buying ability before lol. Ill have to make sure to use this
GreyHead 64
October 10 2012, 13:16 #300886
Hi Tom,

I'm not clear what you are doing in the $vartwo code. But you can probably do
{
$var = $var * $roomsize_array[$form->data['RoomSize']];
}
$var = round( $var * 9 );
$form->data['estimateone'] = $var - 8;
$form->data['estimatetwo'] = $var + 2;
$vartwo = round( $var );
$form->data['estimateone'] = $vartwo - 8;
$form->data['estimatetwo'] = $vartwo + 2;


The code in the second box would need to be
<div class="form_item">
<div class="form_element cf_text"> <span class="cf_text">Based on your chosen criteria, <strong>your cleaning quote guide price is between: £{estimateone} & £{estimatetwo}</strong></span> </div>
<div class="cfclear">&nbsp;</div>
</div>

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 10 2012, 14:00 #300891
Hi Bob,

Ahh I knew I was doing it wrong lol. Cause I was trying
<?php echo $form->data['estimateone'] ?>

and

<?php echo $form->data['estimateone'] = $estimateone ?>


Regarding the $vartwo I have as yet not done anything on this front. But to explain, the numbers in the code block (other than the *9) are all representative of a time frame eg .25 is 15 minutes 1.5 is 1 hour 30 etc. Currently I had been outputting an estimated cost only ($estimateone and $estimatetwo) which are basically worked out by adding all the time frames together and then simply multiplying them by the per hour cost. (and the -8 and +2 is to give a margin for error in the estimation).

What I was hoping to do is to also output the time in a similar manner so people are ever more informed (eg i stop getting phone calls or emails asking how long it might take).

"GreyHead":
Hi Tom,

I'm not clear what you are doing in the $vartwo code. But you can probably do
{
$var = $var * $roomsize_array[$form->data['RoomSize']];
}
$var = round( $var * 9 );
$form->data['estimateone'] = $var - 8;
$form->data['estimatetwo'] = $var + 2;
$vartwo = round( $var );
$form->data['estimateone'] = $vartwo - 8;
$form->data['estimatetwo'] = $vartwo + 2;


The code in the second box would need to be
<div class="form_item">
<div class="form_element cf_text"> <span class="cf_text">Based on your chosen criteria, <strong>your cleaning quote guide price is between: £{estimateone} & £{estimatetwo}</strong></span> </div>
<div class="cfclear">&nbsp;</div>
</div>

Bob


Kindest regards,

Tom.

PS have any translations been done for chronoforms ? If so where where abouts is this info stored please?
H
HSN 74
October 10 2012, 14:09 #300892
Just tried the {estimateone} and {estimatetwo} and these currently do not output anything.

I have tried it with the "purecode" box ticked and unticked.

My php code block is after the multipage element in the onsubmit area so the values should be processed and carried over to be usable on page 2 from page 1.
I have tried turning the drop down from off to On for unique session key on the multipage element in the onsubmit area and the only thing I could consider as being wrong is im using the "Test Form" icon at the top ?

Kindest regards,

Tom.
GreyHead 64
October 10 2012, 14:13 #300893
Hi Tom,

Do you have the Multi-page actions in place to handle the data transfer?

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 10 2012, 14:17 #300894
Yup, they are setup as per your FAQ article I believe.

On Load
--Multipage
--Show Html

On Submit
--Multi Page
--Custom Code - Php Block

On Page Two
--Multi Page
--Show Html

On Page Three
--Multi Page
--Show Html
GreyHead 64
October 10 2012, 15:10 #300896
Hi Tom,

Please take a Form Backup using the icon in the Forms Manager and post it here (as a zipped file) or PM or email it to me and I'll take a closer look.

Bob
ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much
H
HSN 74
October 10 2012, 15:38 #300898
Hi Bob,

Please find the file attached as requested.

Also if you want to take an actual look at the backend of the site let me know and I can arrange this for you.
Attachments
HSN Form Bak.zip
(7.05 KiB)
224 Downloads/Views
H
HSN 74
October 11 2012, 20:18 #300965
Hi Bob,


Just wondering if you have had a chance to take a look as yet and spotted any potential errors im making?

Many thanks.