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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<radio> - pass default value?

Status
Not open for further replies.

sonicsnail

Technical User
Nov 10, 2001
48
IN
I have a form that's getting sent to a database. The part I'm working on just now is for a few yes/no options... basically, they dot one of them for "pass", the other for "fail".

I think I've been clever though and set their values as &quot;red&quot; and &quot;green&quot;, so when it comes to displaying the results, I can use <img src=&quot;<*output*>.gif&quot;> and have a little image for each.

My problem is, if they DON'T choose either pass or fail, then a blank value gets sent to the database, and when the database is rendered, it shows a the broken pic link because its parsing <img src=&quot;.gif&quot;>

here they are...

<input type=&quot;radio&quot; name=&quot;test1&quot; value=&quot;green&quot;>
<input type=&quot;radio&quot; name=&quot;test1&quot; value=&quot;red&quot;>

can I add some sort of default value so that if the user doesn't click one of the radios it passes (for example) &quot;white&quot; to the database?

thanks in advance,

Pete
 
I think the best thing to do is check the value server-side :

<%
my_entry = Request(&quot;input&quot;)

If my_entry = &quot;&quot; Then
my_entry = &quot;white&quot;
End If
%> Regards

Big Dave

davidbyng@hotmail.com


** If I am wrong I am sorry, but i'm only trying to help!! **

 
Use the below function to get the radio value..me wrote it sometime ago

function getRadioValue(obj)
{
for (x=0; x < obj.length; x++) if (obj[x].checked) return obj[x].value;
return -1;

}

obj will be your radio button name. Freedom is a Right
 
David - thanks, but I can't use serverside scripting.. :-(

GoatStudio - nice, but getting the radio value isn't my problem.. my problem is assigning it a default value when they *haven't* been dotted.

??
 
Your only client-side option for this is to either force one of the two options to be selected by default (using the &quot;checked&quot; parameter), or have a third option (none) which is defaulted by using &quot;checked&quot;, or change the fields that you pass to the server to hidden fields, and write an onSubmit function to test the values of the radio buttons and assign the correct values to the hidden text fields depending on which (if any) radio button is checked. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
At the end of the page, you can add this javascript using the getRadioValue(obj) function I provided.

<script language=javascript>
if (getRadioValue(document.form.radio) < 0){
document.form.radio[0].checked = true;
}

</script> ------------------
Freedom is a Right
 
Wouldn't that be essentially the same thing as adding the checked attribute to the first radio button, only more complicated? Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top