• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1063
  • Last Modified:

Struts and Radio button problem

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
cofactor
Asked:
cofactor
  • 5
  • 3
1 Solution
 
cofactorAuthor Commented:
i forgot to add  'onchange'  on each radios...but it is there.
0
 
radarshCommented:
What type is your variable in ActionForm?

________
radarsh
0
 
radarshCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

________
radarsh
0
 
cofactorAuthor 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 ?
0
 
radarshCommented:
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
 
cofactorAuthor 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.
0
 
radarshCommented:
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
 
GranModCommented:
PAQed with points refunded (50)

GranMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now