Forums

Calculating a dropdown list based on a date given in a field and current date

fribse 13 May, 2016
I have a membership form that I would like to refine even further.
Currently people give their data, including their birthday and type of membership, and then at the end, the amount they have to pay, is shown.

I would like to update the membership selector, which is a dropdown, based on the current date, and the birthday the give.
We have four types of memberships:
Senior
Junior
Family
Support
The two first are influenced by the birthday they give, and they are all influenced by the current date.
What I would like is to update the memberships, so it autmatically shows the correct Senior/Junior, and the amount is shown at the same time.
Something like
Senior (750)
Family (1500)
Support (100)
or if the person is young
Junior (500)
Family (1500)
Support (100)
The Family and Senior / Junior are also influenced by the current date, ie. for each quarter it is less as the memberships all run from 1/1->31/12
I can't seem to be able to do anything automatically from the datepicker? Is that true? Or can I do a workaround somehow?
I have the calculation of the fee in PHP.

This is private content

fribse 13 May, 2016
I got this made, which is taken from the demo, don't really know how it's supposed to look like, and it doesn't work of course :-)
First of the dropdown field called 'type' is not populated. Secondly, I would like for the code to only show the right 'Senior' / 'Junior', and not both.

This is private content

GreyHead 13 May, 2016
Hi fribse,

I only see one ChronoForms question in here (the rest are about your specific code).

You can call a JavaScript function when a datepicker selection is made using the On date selected box in the Datepicker element settings. Just add the function name there.

I guess you will want that function to make some kind of Ajax call to get the data updated - or maybe it would be easier to load the data as JavaScript when the form loads.

Bob
fribse 17 May, 2016
Ahh, ok, I have no idea how to code in Javascript, so i guess I have to find another way of getting this done...
fribse 17 May, 2016
Btw. why doesn't the above command populate the dropdown:
echo json_encode($subs[$form->data["type"]]);
?
I've placed it in the 'onLoad' action?
fribse 17 May, 2016
Ok, I found another post about this (google is your friend) (https://www.chronoengine.com/forums/posts/f2/t101287.html).
This explains it, so that even I think I understand it 😀

So I've built this small code:

$Subs = array(
'[0]' => array('medlem' => 'Enkeltperson', 'beskrivelse' => $Senior.' / '.$Junior),
'[1]' => array('medlem' => 'Familie' , 'beskrivelse' => $Family),
'[2]' => array('medlem' => 'Støtte' , 'beskrivelse' => 'Støtte (kr. 100)'),
) ;
print_r(array_values($Subs)) ;
$form->data('medlemstyper') = $Subs ;

But when I use it I get this error:
Can't use method return value in write context
fribse 17 May, 2016
Answer
Bloody h... I need square brackets in the form->data. Now it actually works.
It doesn't calculate with the birthday, but at least it now calculates from the current date when the form is loaded.
This worked as it should:
$form->data['medlemstyper'] = array(
'[0]' => array('medlem' => 'Enkeltperson', 'beskrivelse' => $Senior.' / '.$Junior),
'[1]' => array('medlem' => 'Familie' , 'beskrivelse' => $Family),
'[2]' => array('medlem' => 'Støtte' , 'beskrivelse' => 'Støtte (kr. 100)'),
) ;
This topic is locked and no more replies can be posted.