Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value of radio button if not selected...

Status
Not open for further replies.

booboo0912

Programmer
Jul 31, 2002
75
0
0
US
If a user doesn't choose a value from a list of radio buttons, the value sent to the database is 'undefined'. I'm trying to check to see if the value of the radio button is null/blank/undefined, and if it is, to change the value being sent to the database to 'n/a' or something similar, anything but 'undefined'. This has to be a simple fix, but I can't figure it out...is it Friday yet????

Anyone out there know the solution?
Thanks in advance!
 
Put an extra radio button in your group with a value of 'n/a' or whatever you want and set it to be selected by default. That way if the user neglects to change it there will be a sensible value passed back to the server.
 
This is what happens with radio sets and Checkboxes.
If you don't select anything nothing ,not even the name of the <input> gets passed.
This is different from textareas, select and text boxes where if you don't type anything just a blank value is passed(but at least the name gets created).

For example if you had a textbox [txtinput] and a radio group [radinput] and you dont select anything on the group and don't type anything on the box when you submit the form you will get something like filename.xxx?txtinput=

Now you can do two things.
On the client side: have a default value on the radio group so you are always sure you are passing something.
On the server side : check for the existence of the variable and if doesn't exist then assign a default value before processing on SQL statement.
for example: Cold Fusion has a handy way of checking for these kind of things <CFPARAM >



I don't know what language you are using but certainly there's got to be a way of checking for this things .

good luck


grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Thanks to you both for your replies!!!

Using a radio button with a default value (n/a) is preferred, although that radio button would have to be hidden from the user...which wouldn't be a bad thing.

grtfercho, I've been trying to check for the existence of the variable before submitting to the database. I'm using a Javascript function to set the value to 'n/a' if a value doesn't exist, but I can't figure out the correct value to check for, ie,

if((value == null) || (value == ' ')) {
document.form.radiobttn.value = 'n/a'
}

the values (null & blank) aren't correct...I know the field is blank/undefined...but how do I check for that??

Thanks again to you both!!
 
You have to use a loop and see the value of the 'checked' property.


here's another quick solution.
have a hidden field with the name of your column in the table.
every time you check on a radio have a function that updates the hidden element if nothing gets selected the original value of the hidden field (ie 'n/a') gets passed.

<script>
function changeField(elem,txtname){
var txtelem=eval('document.'+elem.form.name+'.'+txtname);
txtelem.value=elem.value;
alert('Current hidden Value : '+txtelem.value) //this is for debugging only feel free to delete the line.
}

</script>


<body>

<form action=&quot;actiontest.html&quot; name=&quot;testform&quot;>
<input type=&quot;hidden&quot; name=&quot;txtfield&quot; value=&quot;n/a&quot;>
<input type=&quot;radio&quot; name=&quot;radfield&quot; onclick=&quot;javascript:changeField(this,'txtfield');&quot; value=&quot;Cars&quot;>
<input type=&quot;radio&quot; name=&quot;radfield&quot; onclick=&quot;javascript:changeField(this,'txtfield');&quot; value=&quot;Motorcycles&quot;>
<input type=&quot;radio&quot; name=&quot;radfield&quot; onclick=&quot;javascript:changeField(this,'txtfield');&quot; value=&quot;Planes&quot;>
<input type=&quot;radio&quot; name=&quot;radfield&quot; onclick=&quot;javascript:changeField(this,'txtfield');&quot; value=&quot;Boats&quot;>
</form>
</body>

grtfercho çB^]\..
&quot;Imagination is more important than Knowledge&quot; A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top