• 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: Change value of text field based on radio field

Change value of text field based on radio field 3 months 2 weeks ago #42172

  • theworld
  • theworld's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 7
Hello, I'm trying to change the value of a text field (Agerisk2) to a number based on the selection of a radio field (Age)

In the text field additional attributes I have onChange="$Agerisk2"

In the text field default value I have:
//<code>
$Age = [];
$Agerisk2 = null;
 
switch ($Age) {
    case $Age "Under 45 years":
        $Agerisk2 = "0";
        break;
 
    case $Age "45 – 54 years":
        $Agerisk2 = "2";
        break;
 
    case $Age "55 – 64 years":
        $Agerisk2 = "3";
        break;
 
    case $Age "64 years or over":
        $Agerisk2 = "4";
        break;
 
    default:
        $Agerisk2 = null;
        break;
}
//</code>

Could someone please help me with the correct code or some advice?

Thank you
The administrator has disabled public write access.

Change value of text field based on radio field 3 months 1 week ago #42186

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 164
  • Thank you received: 32
You would want to put onchange="myFunction();" in the additional attributes of the Age field to call the 'myFunction' (use your own name for this) which does the work.
The function should go in the CSS and Javascript section of your form and should start with the function name, hers an example I use to compare two fields
function compare_town() {   
 
  var town = document.getElementById("town");
  var city = document.getElementById("city");
 
  if (town.value == city.value ) {
    alert("IMPORTANT\n\nYou have already entered that information in the 'town' field.\nYou do not need to enter it again ");
    //remove the duplicate info
    document.getElementById('city').value = "";
  }
}
o

I wouldn't use switch either simple if else will do it, See examples You can see in the above that I've also set the 'city' value to empty if it contains the same info as the 'town' field. This is not dissimilar to the statement you need where ;
if age = 'under 45' then agerisk = 0 but you would have to work out how to get the age dropdown value from the options
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
Last Edit: 3 months 1 week ago by iceferret. Reason: added info
The administrator has disabled public write access.

Change value of text field based on radio field 2 months 3 weeks ago #42240

  • theworld
  • theworld's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 7
Thank you iceferret, this is helpful and I'll consider using Javascript instead of PHP.

I've been studying Javascript and PHP over the last couple of weeks and comparing if two text fields are equal as you've provided is relatively easy as the intro course deals with that.

What I am having a hard time with specifically is comparing if a radio selection is true then to print it (a specific number, the value I've assigned to that radio value already) and printing/echoing it to another text field.

Are you perhaps able to assist with something more in line with that? The issue being the ID of each radio selection is different.
Last Edit: 2 months 3 weeks ago by theworld. Reason: Typo
The administrator has disabled public write access.

Change value of text field based on radio field 2 months 2 weeks ago #42243

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 164
  • Thank you received: 32
can't do much at the moment, away on a long awaited break B)
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.
  • 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!