PDA

View Full Version : Radio buttons returning "undefined" value in popup window


dean80
10-07-2002, 08:07 PM
Hey all,

Browsed some of the old threads looking for a possible solution to my problem. I was unable to find it after hard search.

Here is my problem:

I have a form in HTML and after being completed will populate a new popup window with the data entered in the form. I have had some success with returning values for textfields, selects, and checkboxes, but radio buttons are returning values as "undefined". The draft version of the form is available at http://www.deandobbs.com/match and feel free to look at the code.

At the start of the script i have variables declared, and I was under the impression that they had a global scope, but can't figure out why they're not working. The function "checkRadio" contains some for-statements to check for the presence of a selection as well. Somehow how there's something not working and the values are left as undefined.

I will be up most of the night beating my head against the wall, so if any of you guys have AIM, my s/n is beandip80. Feel free to IM me if you see me on.

Thanks in advance for any help you might have!
Dean

Mr J
10-07-2002, 08:40 PM
The three lines



contents += "Gender:&nbsp;" + document.entryForm.gender.checked + "<br>"
contents += "Income:&nbsp;" + document.entryForm.income.checked + "<br>"
contents += "Hobby:&nbsp;" + document.entryForm.hobby.checked + "<br>"



Change to

contents += "Gender:&nbsp;" + test + "<br>"
contents += "Income:&nbsp;" + test2 + "<br>"
contents += "Hobby:&nbsp;" + test3 + "<br>"



Replace your checkRadio function for the following




function checkRadio(entryForm) {
for( i=0; i<document.entryForm.gender.length; i++ ){
if( document.entryForm.gender[i].checked==true ){
gender=i

test=document.entryForm.gender[i].value // ADDED

}
}

for( i=0; i<document.entryForm.income.length; i++ ) {
if( document.entryForm.income[i].checked==true ) {
income=i

test2=document.entryForm.income[i].value // ADDED

}
}

for( i=0; i<document.entryForm.hobby.length; i++ ) {
if( document.entryForm.hobby[i].checked==true ) {
hobby=i

test3=document.entryForm.hobby[i].value // ADDED

}
}

for( i=0; i<document.entryForm.newsletter.length; i++ ) {
if( document.entryForm.newsletter[i].checked==true ) {
newsletter=i
}
}
}





Change test, test2, and test3 to something more suitable




:thumbsup:

Mr J
10-07-2002, 08:56 PM
Ps forgot the newsletter line


contents += "Opt-in Newsletter:&nbsp;" + test4 + "<br>"


for( i=0; i<document.entryForm.newsletter.length; i++ ){
if( document.entryForm.newsletter[i].checked==true ){
newsletter=i

test4=document.entryForm.newsletter[i].value //ADDED

}


Ditto with test4

adios
10-07-2002, 09:01 PM
Use a simple method to move the data:

contents += "Gender:&amp;nbsp;" + get_radio('gender') + "<br>"
contents += "Income:&amp;nbsp;" + get_radio('income') + "<br>"
contents += "Hobby:&amp;nbsp;" + get_radio('hobby') + "<br>"
..........
contents += "Opt-in Newsletter:&amp;nbsp;" + get_radio('newsletter') + "<br>"

...and:

function get_radio(name) {
var f = document.entryForm;
for (i=0; i<f[name].length; i++)
if (f[name][i].checked) return f[name][i].value;
}

dean80
10-07-2002, 09:13 PM
Hey J,

Got your very timely replies to my problem, and I can't begin to thank you enough for the help. I have implemented your suggestions and they work like a dream.

Am I correct in assuming your suggestions are a way to call the newly established variables determined by the for-statements? I'm really curious to know the logic behind that.

I have a plethora of other questions regarding this form I'm working on, but I don't want to flood the boards or monopolize your time.

UPDATE: Adios, saw your comparable suggestion which saves some lines of code on the for-statements. For now I've already modified the script according to J's suggestion, so I'll stick with that since it finally works. I have a question for either of you then:

I have a flowchart created to make a match for the user depending on the selections for "gender", "income", and "hobby". The user will be presented with a celebrity (as a joke) for their dreamdate. I have a flowchart of this at http://www.deandobbs.com/match/flowchart.gif to show you this thought process.

Is it safe to say I can just use a series of conditionals to make a match? Again I greatly appreciate your guys' time and effort in collaborating with me thus far. Thanks!

Mr J
10-07-2002, 10:54 PM
Sorry ...... I did not realise that the function checkRadio(entryForm) was purely for the purpose of getting the value of those radio buttons


The version adios supplied is much more viable and neater with less code.

No point in having more code than is needed.

I would use adios's


:thumbsup:

dean80
10-08-2002, 01:05 AM
Thanks for the advice. I'm still searching for a way to have those radios decide which set of info is shown, and I think I'm gonna fall short. I just don't have enough experience under my belt to do it right now so I'm trying to scale back my plans to something more manageable.

adios
10-08-2002, 05:29 AM
Mr J - thanks. :cool:

dean80...

Forgot to close (</select>) that 2nd drop-down; could be causing some problems. Might have compressed that graphic a bit too much too...cleaned thing up a bit, wish I could help with your algorithim but it's not entirely clear what the desire is. Using conditionals is certainly indicated; the question is what structure to utilize (switch/case, if-else, conditional operators), how to write it most efficiently and how to ensure correct decision-making.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Match Maker</TITLE>
.............
<script language="JavaScript">
<!--
// global vars
var firstName = '';
var lastName = '';
var address = '';
var city = '';
var state = '';
var zipcode = '';
var birthday = '';
var emailAddress = '';
var gender = '';
var income = '';
var hobby = '';
var newsletter = '';
var newWin = '';

// validates user info
function checkInfo(f) {
for (var el,e=0; e<f.elements.length; ++e) {
el = f.elements[e];
if ((el.type == 'text' || el.type == 'textarea') && el.name != 'emailAddress' && !el.value) {
alert('Please fill in all fields.');
el.focus();
el.select();
return false;
}
else if (el.type == 'select-one' && el.selectedIndex == 0) {
alert('Please choose an option from each drop-down list.');
el.focus();
return false;
}
}
return true;
}

// validates email address
function checkEmail(field) {
if (!/(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i.test(field.value)) {
alert('Invalid E-mail Address! Please re-enter.');
field.focus();
field.select();
return false;
}
return true;
}

// opens the new result window
var newWin = '';
function openMatchPage() {
var contents = '', f = document.entryForm, s = '&amp;nbsp;';
contents += '<html><head><title>Meet your match</title></head><body>';
contents += 'First Name:' + s + f.firstName.value + '<br>';
contents += 'Last Name:' + s + f.lastName.value + '<br>';
contents += 'Address:' + s + f.address.value + '<br>';
contents += 'City:' + s + f.city.value + '<br>';
contents += 'State:' + s + f.state[f.state.selectedIndex].value + '<br>';
contents += 'Zipcode:' + s + f.zipcode.value + '<br>';
contents += 'Zodiac Sign:' + s + f.zodiac[f.zodiac.selectedIndex].value + '<br>';
contents += 'E-mail Address:' + f.emailAddress.value + '<br>';
contents += 'Gender:' + s + get_radio('gender') + '<br>';
contents += 'Income:' + s + get_radio('income') + '<br>';
contents += 'Hobby:' + s + get_radio('hobby') + '<br>';
contents += 'witty:' + s + f.witty.checked + '<br>';
contents += 'funny:' + s + f.funny.checked + '<br>';
contents += 'bubbly:' + s + f.bubbly.checked + '<br>';
contents += 'shy:' + s + f.shy.checked + '<br>';
contents += 'sensitive:' + s + f.sensitive.checked + '<br>';
contents += 'honest:' + s + f.honest.checked + '<br>';
contents += 'lazy:' + s + f.lazy.checked + '<br>';
contents += 'vain:' + s + f.vain.checked + '<br>';
contents += 'indecisive:' + s + f.indecisive.checked + '<br>';
contents += 'Opt-in Newsletter:' + s + get_radio('newsletter') + '<br>';
contents += '</body></html>';
var features = 'left=100,top=30,width=450,height=450,';
features += 'scrollbars,status,titlebar';
newWin = open('','newWin',features);
newWin.document.write(contents);
}

function get_radio(name) {
var f = document.entryForm;
for (var i=0; i<f[name].length; i++)
if (f[name][i].checked) return f[name][i].value;
return false;
}

function checkRadios(f) {
if (!get_radio('gender') || !get_radio('income') ||
!get_radio('hobby') || !get_radio('newsletter')) {
alert('Please select one from each pair of radio buttons.');
f.gender[0].focus()
return false;
}
return true;
}

function checkform(f) {
if (!checkInfo(f)) return false;
if (!checkEmail(f.emailAddress)) return false;
if (!checkRadios(f)) return false;
return true;
}

//-->
</script>
</HEAD>
<BODY text="000000">
<div align="center">
<FORM name="entryForm" action="javascript:openMatchPage()" method="post"
onsubmit="return checkform(this)">
<TABLE cellspacing="1" cellpadding="2" bgcolor="000000">
............
<TD>Your sign?</TD>
<TD><SELECT NAME="zodiac">
<option value="">Select Sign</option>
<option value="capricorn">Capricorn</option>
<option value="aquarius">Aquarius</option>
<option value="pisces">Pisces</option>
<option value="aries">Aries</option>
<option value="taurus">Taurus</option>
<option value="gemini">Gemini</option>
<option value="cancer">Cancer</option>
<option value="leo">Leo</option>
<option value="virgo">Virgo</option>
<option value="libra">Libra</option>
<option value="scorpio">Scorpio</option>
<option value="sagittarius">Sagittarius</option>
</select>
</TD>
..........
<INPUT TYPE="image" src="reset.gif" align="left">
<INPUT TYPE="image" src="go.gif" align="right"></TD>
</TR>

</TABLE>

</FORM>
</div>
</BODY>
</HTML>

dean80
10-08-2002, 02:06 PM
Adios,

The idea is to have the 3 radio buttons (gender, income, hobby) choose the user's match based on their selections. I posted a link to a flowchart in a previous post in this thread to try to illustrate what the thought process should be.

As for what type of statements to use, that's where I'm totally lost. I don't know whether if/else, switches or conditionals is the approach to take. Eventually the idea would be to have the user fill out the form and based on the info provided it would dynamically create a popup page with the celebrity, a brief write-up of why they're your match, and your zodiac signs. Each celebrity has a zodiac sign and based on the user's sign a link to a page on astrology.com would be displayed. IE: kid rock is a capricorn and you're an aries, so a link for how capricorn and aries interact is shown and if you want to read about it you can follow the link to astrology.com.

What's making me spin my wheels is that I have too many unknowns and not enough knowledge right now to tackle all these issues. I've been working with JS for about 6 weeks thus far.

I guess what's needed is something like this: a form that validates info, once it's validated properly, a popup is created that shows your match. The popup has an image of the celebrity, a brief text description of why you match, with some of the user's information contained in that body of text. Below the text would be the zodiac info talked about earlier, and user's contact info (Name, address, etc).

How to do this? Not a clue. I guess I could try to make a string variable that contains HTML to do the layout of the popup, but as to populating the dynamic portions of the page, I've bitten off more than I can chew. Sorry about the long message, but I figured I'd try to give you as many details as I can.

Thanks for all your patience thus far!
Dean