• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to get radio button value

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to get radio button value which is selected by the use of
request.getParameter();
I am getting null value here.
Below is my code for radio button:
<tr><td><p>Base Component: </p></td> <td><table cellspacing="0" cellpadding="0">
<input id=" " name="BaseOption" value="" type="radio" checked='checked' /><label for="no"> No</label>
<input id=" " name="BaseOption" value=" " type="radio" /><label for="yes">Yes</label>
</table></td> </tr>

i m retrieving the value which is clicked by:

if(request.getParameter("base") != null) {
if(request.getParameter("base").equals("Yes")) {
out.println("Radio button yes was selected.<BR>");
}
else {
if(request.getParameter("base").equals("No")){
out.println("Radio button no was selected.<BR>");}
}
}

here i m getting null value
request.getParameter("base") is giving me null value. Please let me know how to get radio button value
 
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shalu,
use request.getParamValues.

Regards,
Vinoth.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shalu pareek:
<input id=" " name="BaseOption" value="" type="radio" checked='checked' /><label for="no"> No</label>
<input id=" " name="BaseOption" value=" " type="radio" /><label for="yes">Yes</label>



you are giving value="" for both yes and No . you should give
value="yes" and value="no" then only you can get the value according to the radio button right?
 
shalu pareek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we use this method in jsps.
It will retrun be some array of values so the condition which i am using wont work for this....
 
shalu pareek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry but i am using yes or no too
in the code

my mistake to put the worng code
but this is giving me null value only
 
Vt Guru
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya it will written a value which you are selected and it won't give any value for non selected item. So just create a hidden field to get all the element and by comparing you can find the fields i did the same for my pro its working fine.
 
shalu pareek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am soory but i am not that good. so if you can place the code it will be better to understand that.
 
shalu pareek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still i am getting null value by the use of req.getParameterValues("");
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shalu pareek:
still i am getting null value by the use of req.getParameterValues("");



please read my above post and use request.getParameter("radio_name");
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you get null or an empty value? There's quite a big difference in between those values.

You specified an empty value in the radio button: value="". You need to specify a non-empty value here.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !

<input id=" " name="BaseOption" value="" type="radio" checked='checked' /><label for="no"> No</label>
<input id=" " name="BaseOption" value=" " type="radio" /><label for="yes">Yes</label>


And you used "request.getParameter("base")". Try to retrive that parameter using radio button name as "BaseOption" and give value also.
[ December 22, 2008: Message edited by: Venki Ananth ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know, that this topic is quite old, but, when I was finding answers here, someone else can too... So...
If you're sure, that your is part of form, than you can get value of currently selected radio button by simply using HttpServletRequest request function request.getAttribute("radio")

if you are using some form like this you should use )

In this case you'll get "banana" (of course If you choose it). If you change in element input attribute to value="", than you obtain null.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Batelka wrote: than you can get value of currently selected radio button by simply using HttpServletRequest request function request.getAttribute("radio")


This is not correct!

The setAttribute() method has nothing at all to do with form parameters. You need to use getParameter() or one of the other parameter-focused methods to retrieve form submissions.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Batelka wrote:if you are using some form like this you should use )


Be aware the getPart() is new with Servlets 3.0 which is not yet in wide-spread use.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For accessing the checkbox values in servlet . You need to call the getParamatervalues("BaseOption") on the request object and this return a Sting[] .Now you have the checked values .

.

 
reply
    Bookmark Topic Watch Topic
  • New Topic