• 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: Calculate with radio buttons

Calculate with radio buttons 10 years 10 months ago #27855

I have built a quiz type of form with 13 questions. Each is on a separate page with a pagebreak taking the user to the next page. Each question only has 2 possible answers, assigned values of "2" and "1". Questions are called qa01 - 13.

I have used the script found on demo.rsjoomla.com/calculation-form-example and modified it to suit my form. My whole script is:
<script>
function calculateRadio(){
 
var op1=document.getElementsByName('form[qa01]');
var op2=document.getElementsByName('form[qa02]');
var op3=document.getElementsByName('form[qa03]');
var op4=document.getElementsByName('form[qa04]');
var op5=document.getElementsByName('form[qa05]');
var op6=document.getElementsByName('form[qa06]');
var op7=document.getElementsByName('form[qa07]');
var op8=document.getElementsByName('form[qa08]');
var op9=document.getElementsByName('form[qa09]');
var op10=document.getElementsByName('form[qa10]');
var op11=document.getElementsByName('form[qa11]');
var op12=document.getElementsByName('form[qa12]');
var op13=document.getElementsByName('form[qa13]');
var result=document.getElementById('score');
 
result.value=0;
 
result.value=parseInt(result.value);
 
for(i=0;i<op1.length;i++)
 
if(op1[i].checked) result.value=parseInt(result.value)+parseInt(op1[i].value);
 
for(i=0;i<op2.length;i++)
 
if(op2[i].checked) result.value=parseInt(result.value)+parseInt(op2[i].value);}
 
</script>

This script will only add up the values of the first 2 radio buttons. I originally inserted the script with the following addition:
for(i=0;i<op3.length;i++)
 
if(op3[i].checked) result.value=parseInt(result.value)+parseInt(op3[i].value);}
 
for(i=0;i<op4.length;i++)
 
if(op4[i].checked) result.value=parseInt(result.value)+parseInt(op4[i].value);}
 
for(i=0;i<op5.length;i++)
 
if(op5[i].checked) result.value=parseInt(result.value)+parseInt(op5[i].value);}
 
for(i=0;i<op6.length;i++)
 
if(op6[i].checked) result.value=parseInt(result.value)+parseInt(op6[i].value);}
 
for(i=0;i<op7.length;i++)
 
if(op7[i].checked) result.value=parseInt(result.value)+parseInt(op7[i].value);}
 
for(i=0;i<op8.length;i++)
 
if(op8[i].checked) result.value=parseInt(result.value)+parseInt(op8[i].value);}
 
for(i=0;i<op9.length;i++)
 
if(op9[i].checked) result.value=parseInt(result.value)+parseInt(op9[i].value);}
 
for(i=0;i<op10.length;i++)
 
if(op10[i].checked) result.value=parseInt(result.value)+parseInt(op10[i].value);}
 
for(i=0;i<op11.length;i++)
 
if(op11[i].checked) result.value=parseInt(result.value)+parseInt(op11[i].value);}
 
for(i=0;i<op12.length;i++)
 
if(op12[i].checked) result.value=parseInt(result.value)+parseInt(op12[i].value);}
 
for(i=0;i<op13.length;i++)
 
if(op13[i].checked) result.value=parseInt(result.value)+parseInt(op13[i].value);}

after the line containing op2 and before the < /script > line. Doing this caused the form to freeze when the last "Next" was clicked, and does not proceed to the next page which is where I display the score.

I'm sure I've made some little blunder, not being a programmer, but I cannot discern what it is. Some expert insight would be appreciated - thanks!
The administrator has disabled public write access.

Redirect to different URLs based on total of radio 10 years 10 months ago #27856

This is related to www.rsjoomla.com/forum/37-rsform-pro/241...h-radio-buttons.html. I have (or will have) a number of radio buttons being totalled to give a score. I want, when the user clicks, to redirect them to one of 3 different URLs depending on the score. I'd hoped to use conditional fields depending on the number in the field "score" (where the total of the radio buttons is placed), but I don't get that option.

I'm sure there's a JS or PHP way to accomplish this easily.
The administrator has disabled public write access.

Redirect to different URLs based on total of radio 10 years 10 months ago #27859

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
An easier approach to your case would be to use the "Form Calculations" feature. More information about this feature can be found at the following url:
http://www.rsjoomla.com/support/documentation/view-article/909-form-calculations.html

Keep in mind that the correct syntax for radio items should be similar to

Right[p1]
Wrong[p0]


You can set up a redirect based on the total value in numerous ways, one way would be to create 2 hidden fields, one to store the result and one to store the redirect url. The code should look like the one below:
if($_POST['form']['total_hidden_field'] > 'input_value'){
	$_POST['form']['url'] = 'http://firstredirect';
}else if ($_POST['form']['total_hidden_field'] < 'input_value'){
	$_POST['form']['url_hidden_field'] = 'http://secondredirect.com';
}else{
	$_POST['form']['url_hidden_field'] = 'http://thirdredirect.com';}

Head over to Form properties > Form Info and in the Return URL field use the {url_hidden_field:value} placeholder.
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.

Redirect to different URLs based on total of radio 10 years 10 months ago #27867

  • paypal14
  • paypal14's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 21
On quick inspection I think I can work through this. The only thing I'm not sure about is the redirects work on a score range - URL 1 = a score of 13-16, URL 2 = score of 17 - 21, and URL 3 = score of 22 - 26. So I am not testing for a single number - do I do this with boolean? I'm not sure how to code this.

Otherwise thanks for the great, and quick, response.
The administrator has disabled public write access.

Redirect to different URLs based on total of radio 10 years 10 months ago #27873

  • paypal14
  • paypal14's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 21
OK. I have a visible field (called "score) which has the caption "Your score is:" and the calculation is adding correctly, though it's showing with 2 decimal points. How do I make it just an integer?

Also I'm still confused with the code for the second field. My field which does the calculation is called "score". As noted in my previous post, I need to check for 3 separate ranges - 13-16, 17-21, and 22-26 (all inclusive).

My code for this hidden field is currently lookin glike this:
if($_POST['form']['total_hidden_field'] > 'input_value'){
	$_POST['form']['url'] = 'http://www.insightstrategies.com/quiz/quiz01/red';
}else if ($_POST['form']['total_hidden_field'] < 'input_value'){
	$_POST['form']['url_hidden_field'] = 'http://www.insightstrategies.com/quiz/quiz01/yellow';
}else{
	$_POST['form']['url_hidden_field'] = 'http://www.insightstrategies.com/quiz/quiz01/green';}
The administrator has disabled public write access.

Redirect to different URLs based on total of radio 10 years 10 months ago #27876

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
You can remove the decimals from the result by Components > RSForm!Pro > Configuration > Calculations > Decimals set to 0
 
// Define the score range
$firstrange = range(13, 16);
$secondrange = range(17, 21);
$thirdrange = range(22, 26);
 
// Using if and else if , we check if the field that holds the score matches the range defined above
 
if(in_array($_POST['form']['score'], $firstrange)){
  $_POST['form']['url'] = 'http://firstredirect.com';}
   else if (in_array($_POST['form']['score'], $secondrange)){
  $_POST['form']['url'] = 'http://secondredirect.com';}
   else if (in_array($_POST['form']['score'], $thirdrange)){
  $_POST['form']['url'] = 'http://thirdredirect.com';}
 
 

Please note, the hidden field that will hold the redirect has the name url and the field which stores the result is called score
My help is not official customer support. To receive your support, submit a ticket by clicking here
Last Edit: 10 years 10 months ago by cosmin.cristea.
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!