We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Struts and Radio button problem

cofactor
cofactor asked
on
Medium Priority
1,075 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








Comment
Watch Question

Author

Commented:
i forgot to add  'onchange'  on each radios...but it is there.

Commented:
What type is your variable in ActionForm?

________
radarsh

Commented:
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

Commented:
Your form control (ActionForm variable) should be a String

________
radarsh

Author

Commented:
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 ?

Commented:
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

Author

Commented:
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.

Commented:
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
Commented:
PAQed with points refunded (50)

GranMod
Community Support Moderator

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.