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

Radio button variables

Status
Not open for further replies.

NorseOne

Programmer
Apr 11, 2007
2
CA

Hi there

I'm trying to set up form validation and confirmation by way of focussing on the field(s) not completed (one at a time, early one first). Then once the form is completed, there is a confirmation of entered values listed (alert). I'm not sure how to handle the radio button variable name="Card_Type". The value is "undefined". It's the last thing I need to do and I'm finished! (Note: I left out extraneous code to make it easier to analyze.)


<script type="text/javascript" language="JavaScript">
function validateForm(pageForm) {
if (!pageForm.Card_Type[0].checked && !pageForm.Card_Type[1].checked && !pageForm.Card_Type[2].checked) {
pageForm.Card_Type[0].focus() ;
return false;
}
var entry = pageForm.Card_Type.value
var r=confirm(entry)
if (r==false) {
alert("Please return to form" + '\n' + "and make corrections.");
return false ;
}
return true;
}
</script>

Many thanks.

 
- Do the radio buttons all have the same name / id (depending on your DOCTYPE)?

- Are you using square brackets ([, ]) in your names / IDs to satiate a PHP back end? If so try removing them.

- Do you have more than one radio buon with a name / ID of "Card_Type"?

- Can you post your form's radio button code?

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,
Thanks for your reply. I am a bit new to javascript. Here is more info. No php is involved on my end. That's the problem I'm trying to resolve: to get the variable to display the value of the selected radio button, instead of "undefined".

The following line is where I'm stuck:

Code:
var entry = pageForm.Card_Type.value

Also,

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html1/DTD/loose.dtd">[/URL]

(in the form section)

Code:
Credit Card Type:
Visa<input type="radio" name="Card_Type" value="Visa" id="Visa">

Mastercard<input type="radio" name="Card_Type" value="Mastercard" id="Mastercard">

American Express<input type="radio" name="Card_Type" value="AmericanExpress" id="AmericanExpress">
 
Because you have multiple radio buttons, you can't just ask for the value in JS... you have to iterate over all radio buttons, finding out which is selected, and then getting its value.

Server-side, you should be OK, because AFAIK, only the selected value is sent with a form submission.

So, having said that, maybe you don't need to do this in JS (simply do it server-side). If you do need to loop around them client-side, something similar to this should work:

Code:
var radios = pageForm.Card_Type;
for (var loop=0; loop<radios.length; loop++) {
   // here you can access radios[loop].propertyName to check for selection and value, etc
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top