Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

radio button value

Posted on 2016-10-29
3
Medium Priority
?
124 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
[X]
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
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 2000 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 59

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

610 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