• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Populate hidden field based on calculation field

Populate hidden field based on calculation field 1 year 2 months ago #42445

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
HI Everyone

I need some guidance as not great with Javascript or PHP but need to do some custom scripting in a form.

I have a hidden calculation field that adds up several other fields and turns it into a percentage - all working great!

The values are returned as 0.23, etc depending on the calculation

I have a hidden text field that I want to populate with one of five pieces of text based on the percentage range of the hidden calc field

i.e

calcfield value = .16

textfield should populate as follows

If calcfield is equal to or greater than 0 and equal to or greater than 0.20, then textfield = "text1"
If calcfield is equal to or greater than 0.21 and equal to or greater than 0.40, then textfield = "text2"
If calcfield is equal to or greater than 0.41 and equal to or greater than 0.60, then textfield = "text3"
If calcfield is equal to or greater than 0.61 and equal to or greater than 0.80, then textfield = "text1"
If calcfield is equal to or greater than 0.81 and equal to or greater than 1.0, then textfield = "text1"

This field needs to poplulate before the form submits and the result needs to show in the emails and the pdf

Can anyone advise:

should I use Javascript or PHP for this
If PHP, which PHP script area should it be placed in

Anyone who has a sample of how to code this, it would be much appreciated.

I know how to show text in the emails but I don't know how to calculate the field and have it submit with the form in time to go in the emails and pdf

Thanks

Paws
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42446

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 200
  • Thank you received: 48
Some of at doesn't make sense but i'm assuming you are asking for the text to change when between values i.e greater than 0 and less than 0.2?
If that's the case I'd use PHP in the 'PHP scripts/scripts called on form process section' - try this, obviously changing the $textfield values to reflect what you want it to be
 
$calcfield = $_POST['form']['calcfield'];
 
//if between 0 - 0.2 or between 0.61 - 1 
if($calcfield >= 0 && $calcfield <= 0.2 || $calcfield >= 0.61 && $calcfield <= 1) {$textfield = 'between 0 and 0.2 or between 0.61 and 1';	
}
 
//if between 0.21 - 0.4
elseif($calcfield >= 0.21  && $calcfield <= 0.4) {$textfield = 'between 0.21 and 0.4';
}
 
//if between 0.41 - 0.6 
elseif($calcfield >= 0.41  && $calcfield <= 0.6) {$textfield = 'between 0.41 and 0.6';
}
 
//assign value of $texfield to hidden form field
$_POST['form']['textfield'] = $textfield; 
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
Last Edit: 1 year 2 months ago by iceferret. Reason: adding comments to code
The administrator has disabled public write access.
The following user(s) said Thank You: info5616

Populate hidden field based on calculation field 1 year 2 months ago #42447

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Thank you so much Iceferret

I'm sorry if some of it did not make sense (I forgot to edit some of the textfield values after cutting and pasting them) but you have interpreted what I was trying to do perfectly and I can correct it from there.

I will try it out and let you know how I went but this gives me an idea now of what I am doing

Thanks, thanks, thanks.

Cheers

Jacqui
Last Edit: 1 year 2 months ago by info5616.
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42449

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 200
  • Thank you received: 48
No scrub that, I realised in the midddle of last night it won't work because the $calcfield won't have a value to work with when the form is processed. To do what you want the calculation is going to have to be done in the scripts called on form process as well so i've modified the code to show how it would be done if you had two text fields where numbers selected from two dropdowns e.g 5 and 15 or text fields with user input, comment out or delete whichever you're not using
//this line for dropdowns or radio
$calcfield = (($_POST['form']['fieldA'][0] + $_POST['form']['fieldB'][0])/100); 
//this line for user input in text field
//$calcfield = (($_POST['form']['fieldA'] + $_POST['form']['fieldB'])/100);
 
//if between 0 - 0.2 or between 0.61 - 1 
if($calcfield >= 0 && $calcfield <= 0.2 || $calcfield >= 0.61 && $calcfield <= 1) {$textfield = 'between 0 and 0.2 or between 0.61 and 1';	
}
 
//if between 0.21 - 0.4
elseif($calcfield >= 0.21  && $calcfield <= 0.4) {$textfield = 'between 0.21 and 0.4';
}
 
//if between 0.41 - 0.6 
elseif($calcfield >= 0.41  && $calcfield <= 0.6) {$textfield = 'between 0.41 and 0.6';
}
 
//assign value of $texfield to hidden form field
$_POST['form']['textfield'] = $textfield;
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
Last Edit: 1 year 2 months ago by iceferret. Reason: amendment
The administrator has disabled public write access.
The following user(s) said Thank You: info5616

Populate hidden field based on calculation field 1 year 2 months ago #42450

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Thanks Iceferret

I will try this instead

Cheers

Witchypaws
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42500

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Hi Iceferret

I can't get the calculation field to work.

Each of the fields it pulls off are configured single checkbox fields (due to how they wanted the form laid out) and the item configuration on all of them is as follows

Anti-Bullying, Harassment & Discrimination|Anti-Bullying, Harassment & Discrimination[p1] and the same for the other six fields

This assigned each checked field a value of 1 and was working when I used the RSFormsPro calculations function

However, when I try to create the calc field manually in the script called on form process with this code below, I don't get any value in the calc field

$CalcPoliciesRequiredEmployees = ($_POST + $_POST + $_POST + $_POST + $_POST + $_POST + $_POST);

The form submits but there is no value showing in the submissions

Can you help please.

Thanks

Jacqui
Last Edit: 1 year 2 months ago by info5616. Reason: Left something out
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42502

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 200
  • Thank you received: 48
Hi Jaqui....so you have a total of seven separate checkbox fields each with one value (checkbox) which the user ticks or not as the case may be?
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42504

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Hi Iceferret

Yes, that is correct.

I also just realised that the code I posted yesterday got stripped out by the forum, so I have now inserted it correctly.
$CalcPoliciesRequiredEmployees = ($_POST['Wellbeing Health Check']['Anti-Bullying Harassment Discrimination'] + $_POST['Wellbeing Health Check']['Code of Conduct'] + $_POST['Wellbeing Health Check']['Equal Employment Opportunity'] + $_POST['Wellbeing Health Check']['Incident and Injury Reporting'] + $_POST['Wellbeing Health Check']['Privacy'] + $_POST['Wellbeing Health Check']['Use of Electronic Media - IT'] + $_POST['Wellbeing Health Check']['Work Health and Safety']);

Each of the field values is based on the price part of the field and each is assigned a value of 1

I have tried useing
'Code of Conduct':value and 'Code of Conduct':price
but I must not have had the syntax right because sometimes I cannot get the form to advance to the second page (it is a multipage form) and sometimes, I can get it advance but the calc field does not register a value in the submission.

Your assistance is most appreciated.

Thanks

Jacqui
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42505

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Hi Iceferret

I tried to click the link in your signature to contact you direct but it says the server does not exist.

If you wanted to contact me on This e-mail address is being protected from spambots. You need JavaScript enabled to view it , I would be happy to discuss commissioning you to write the code for me in the form if you are interested.

Cheers

Jacqui
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42508

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 200
  • Thank you received: 48
Mail sent and signature link fixed :)
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 2 months ago #42509

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Hi Iceferret

Excellent have sent reply

Cheers

Jacqui
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 1 month ago #42530

  • khnoiel
  • khnoiel's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 7
Hi...

can you post the form please?

this is an example i also need... :)
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 1 month ago #42542

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 200
  • Thank you received: 48
The solution was pretty straightforward. In the php called on form process create an array of the selectable options, using a foreach loop step through them and increase a counter by 1 for each that is selected by. the user, quick bit of maths gave a figure which then got used in some if....ifelse fields to pass some requiredtext to a hidden field depending on the value i'e
if $a>0 or $a<0.2 $text = 'text 1'
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
The administrator has disabled public write access.

Populate hidden field based on calculation field 1 year 1 month ago #42549

  • info5616
  • info5616's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 8
Hi khnoiel

I can't post the form as it belongs to a client and I don't have time to change 48 questions and anonymise them.

However, I have spoken with Iceferret and he is happy for me to share the code he developed for me (I have left his copyright in so you can attribute if you wish)
 
//*================================================================================
*Custom code by hedgerow Wizard ©2023
*https://www.hedgerow-wizard.co.uk
*email: mike@hedgerow-wizard.co.uk
*================================================================================*/
 
//call function testingcalcs() if selected dropdown options are ?
if($_POST['form']['dropdown'][0] == 'option1' || $_POST['form']['dropdown'][0] == 'option2') {
testingcalcs();
}
 
//FUNCTIONS========================================================
 
//function called on testingcalcs fields
function testingcalcs() {
 
//declare variable, set to zero
$total = 0;
 
//create array from form fields - only change the text in the second set of square brackets to suit field names
//to add a field copy, paste and rename before the closing );
//to remove a field simply comment out with //
$calcs = array(
	$_POST['form']['fieldA'],
	$_POST['form']['fieldB'],
	$_POST['form']['fieldC'],
	$_POST['form']['fieldD'],
	$_POST['form']['fieldE'],
	$_POST['form']['fieldF'],
	$_POST['form']['fieldG']
//paste new field here
);
 
//loop through the array
foreach ($calcs as $result) {
  //if option is selected (isset) increase counter by 1
	if (isset($result)) {
		$total += 1;
	}
}
 
$calcfield = $total/7; //7 is the number of checkbox fields, amend when adding or removing fields
 
//round to two decimal places   
$calcfield = round($calcfield, 2);
 
//if between 0 - 0.2
if ($calcfield >= 0 && $calcfield <= 0.2) {
	$textfield = 'There\'s a lot of work to be done';
}
 
//if between 0.21 - 0.4 
elseif ($calcfield >= 0.21 && $calcfield <= 0.4) {
	$textfield = 'You\'ve made a start';
}
 
//if between 0.41 - 0.6
elseif ($calcfield >= 0.41 && $calcfield <= 0.6) {
	$textfield = 'You\'re getting there, keep going';
}
 
//if between 0.61 - 0.8
elseif ($calcfield >= 0.61 && $calcfield <= 0.8) {
	$textfield = 'You\'re nearly there';
}
 
//if between 0.81 - 1
elseif ($calcfield >= 0.81 && $calcfield <= 1) {
	$textfield = 'Congratulations, You\'ve arrived';
}
 
//assign value of $texfield to hidden 
$_POST['form']['CalcField1'] = $textfield;
$_POST['form']['TextField1'] = $calcfield;
 
}
//END TESTINGCALC FUNCTION
 

Hope that helps

Jacqui
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!