Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get radio button value  RSS feed

 
shalu pareek
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shalu,
use request.getParamValues.

Regards,
Vinoth.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Vinoth Thirunavukarasu
Ranch Hand
Posts: 164
Android Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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");
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • 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.
 
Venki Ananth
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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 ]
 
Martin Batelka
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Shyam Ramath
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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 .

.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!