[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Struts and Radio button problem

Posted on 2006-03-26
10
Medium Priority
?
1,059 Views
Last Modified: 2008-01-09
I have  a struts  question.



Objective:
==========

a) I need  to  have 3  radio button.

b) when you select any radio,others should be unselected  i.e only one radio button can be chosen at any point of time

c) onchange of any radio button i want to submit the form.

d) Initially,the first radio  button would be selected.






Problem  I Faced
=================



i am in trouble to maintain  these things with radios.....

procedure 1
============

if i use

<html:radio  property="radiobutton" value="abc">
<html:radio  property="radiobutton" value="def">
<html:radio  property="radiobutton" value="ijk">


I cant submit the form onchange of radios......reason is, if i choose second radio  javascript would prompt "undfined" becoz it wont dstinguish the different radios




procedure 2
============

if i use

<html:radio  property="radiobutton1" value="abc">
<html:radio  property="radiobutton2" value="def">
<html:radio  property="radiobutton3" value="ijk">


thoug javascript would allow submit  form onchange of radios but i cant uncheck other radios upon selection of another radios.





Question To you guys
====================

can you tell me the solution of my objective ?
dont think about my approaches/procedures because those were trials which was fruitless. I think, there must be some standard way to solve this kind of problem.

how to solve this problem ?

thank you








0
Comment
Question by:cofactor
  • 5
  • 3
9 Comments
 

Author Comment

by:cofactor
ID: 16294602
i forgot to add  'onchange'  on each radios...but it is there.
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16294821
What type is your variable in ActionForm?

________
radarsh
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16294885
First of all, a radio button is used to select only one value.
So, you must follow the procedure 1 in which you have the same property defined for all three buttons.

Now, how are you trying to submit using JavaScript?
It should be onchange="document.FormName.submit();"
Why do you need to distinguish the radios?

All the three radio buttons together represent one input.

Take an example of gender. It can be M or F. And we can provide two radio buttons for that.
each with property equal to gender. Values will be M and F. Now, what gets submitted is
actually gender=M or gender=F.

I guess yours is a similar scenario. Can't you distinguish the value at the server side?

Correct me if I have gotten your question wrong.


________
radarsh
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:radarsh
ID: 16294891
Your form control (ActionForm variable) should be a String

________
radarsh
0
 

Author Comment

by:cofactor
ID: 16297008
feedback
======
>What type is your variable in ActionForm?

i have DynaActionForm and all  properties are 'String".

>Why do you need to distinguish the radios?

I need it.....because i am sending the value NOT to Action but to  a sinple JAVA class. I am using AJAX(DWR).

Anyway,  i need the value of the radio the user has chosen. once i  get  the value of the radio and i submit this

<script>
function submitMe()
{

var val=document.forms[0]radio.value // this prompts error

MyjavaClass.getValue(val)

// blah blah


}
</script>

and i call the above function from the onchange of radio

As  i told  '//this prompts error", because javascript cant extract the  value !  because all  the radio have same name and so JS has got confused and so it throws "undefined error"

I am stuck here.


N.B:  sorrry, to use the word "submit' the form.....actually it is not the "submit"  but calling the java  classs onchange of radio button as above.


But the problem is JS is not allowing to extract the radio value

How to come around it ?
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16297266
I guess you have problems with your JS

It should be:

function submitMe()
{

var val=document.forms[0].radiobutton.value
MyjavaClass.getValue(val)

// blah blah


}


radiobutton is the name of the property attribute of <html:radio...>

Well, what are you trying to do by MyjavaClass.getValue(val)?
Are you trying to invoke Java code from JavaScript?

Remember Java code is server side code. And JavaScript is client side. You just can't do that
if that's what you are trying to do.

________
radarsh
0
 

Author Comment

by:cofactor
ID: 16298819
again that was a silly mistake .

yes you are right
var val=document.forms[0].radiobutton.value

its my spell mistake.

sorry


>Remember Java code is server side code. And JavaScript is client side. You just can't do that
>if that's what you are trying to do.

 i do it always  :-)   ....... i enjoy it . had it not been a radio , i would have become lucky.
anyway, i changed this radio to a select box and now this works fine.

However, i could not solve it using radio.

 you can call a java class from Javascript , please have a look here

http://getahead.ltd.uk/dwr/documentation

I am using this.
0
 
LVL 10

Expert Comment

by:radarsh
ID: 16298888
That's cool!! I'm new to AJAX. Well, thanks for the link :)

Probably this is one instance where the Answerer has benefited more than the Questioner ;)

________
radarsh
0
 

Accepted Solution

by:
GranMod earned 0 total points
ID: 16655845
PAQed with points refunded (50)

GranMod
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will be showing you how to subnet the easiest way possible for IPv4 (Internet Protocol version 4). This article does not cover IPv6. Keep in mind that subnetting requires lots of practice and time.
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question