/    Sign up×
Community /Pin to ProfileBookmark

garbage from form????

so I have this line

[code]
alert (document.register.cctype.value);
[/code]

and

[code=html]
<input type=”radio” name=”cctype” alt=”Discover” value=”1″>
<input type=”radio” name=”cctype” alt=”Mastercard” value=”2″>
<input type=”radio” name=”cctype” alt=”Visa” value=”3″>
[/code]

when ever I submit the form the alert says undefined, this happens if i check a radio button or not. Does anybody know why this is happening???

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 14.2005 — Yep. When you give more than one form element the same name, you create what is called a "control array" -- which, as you would expect, must be treated the same as any other array. In other words, you must subscript it:

alert (document.register.cctype[0].value);

alert (document.register.cctype.length);
Copy linkTweet thisAlerts:
@KorApr 14.2005 — The value sent to the server, in case of radio buttons is the checked radio button's (if any) value whithin the collection (the collection is much the same as an array).

So that, if you wanna find out the value of the radio buttons collection, use a loop:

[code=php]
function showValue(){
var rads = document.forms['register'].elements['cctype'];
for(var i=0;i<rads.length;i++){
if(rads[i].checked){
alert('The value is: '+rads[i].value);break
}
else{
if(i==rads.length-1){
alert('There is no checked radio thus no value is to be sent')
}
}
}
}
[/code]
×

Success!

Help @DARTHTAMPON spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...