Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

radio button value

Posted on 2016-10-29
3
85 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 43

Accepted Solution

by:
Rob 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 55

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 43

Expert Comment

by:Rob
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

809 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