Solved

radio button value

Posted on 2016-10-29
3
46 Views
Last Modified: 2016-10-31
If an html page contains several radio buttons with the same name (but different values) and NONE is selected (checked), is the value of the radio button element null?

For example, in a form named "st", radio button name "pid", does document.st.pid.value == ""?
0
Comment
Question by:Richard Korts
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 41865445
if none are selected then yes it's null (not empty string)

What happens for you when you try it?
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41865471
It's more than null it is not defined.

Consider this code
<form action="reflect.php" method="post">
   <input type="radio" name="testradio" value="1" />
   <input type="radio" name="testradio" value="2" />
   <input type="radio" name="testradio" value="3" />
   <input type="submit">Submit</input>
</form>

Open in new window

reflect.php
<?php
echo "<pre>" . print_r($_POST, true) . "</pre>";

Open in new window


If you don't click any radios you get an empty $_POST array.

There is a trick you can do to get around this

<form action="reflect.php" method="post">
   <input type="radio" name="testradio" value="0" checked />
   <input type="radio" name="testradio" value="1" />
   <input type="radio" name="testradio" value="2" />
   <input type="radio" name="testradio" value="3" />
   <input type="submit">Submit</input>
</form>

Open in new window


In the above we add a 4th input that acts as a default. This is checked by default and if no other buttons are selected then you get a value of 0 coming through.

You can optionally hide this additional radio with styling so it is not visible. It can be useful though if you want the option to de-select a radio - i.e. being able to "change your mind" after clicking a radio to effectively be "none of the above" = which is not practically possible with a standard bank of radio buttons showing only available answers where "changed my mind" would require being able to click "off" a selected radio the way you would a checkbox.
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 41865481
yes on the server side it will not come through (checkboxes) are the same.

Another trick is to include a hidden element with the default value:

<form action="reflect.php" method="post">
   <input type="hidden" name="testradio" value="0"  />
   <input type="radio" name="testradio" value="1" />
   <input type="radio" name="testradio" value="2" />
   <input type="radio" name="testradio" value="3" />
   <input type="submit">Submit</input>
</form>

Open in new window


However, you are correct that on the client side it will be returned as the empty string "", not null
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now