2
   

Multiple choice quiz in Java Script

 
 
Reply Sat 22 Nov, 2003 04:49 pm
OK, here's the problem. I remember I once had some bits of code allowing to process data from HTML forms through Java scripts and display results. However, I lost them and I don't remember how I made that. I've been trying, but it either displays nothing or just 'undefined' when I try to access it.

Basically, there's that

<Input type="radio" name="q1" value="a">option1
<Input type="radio" name="q1" value="b">option2

then it goes like

<Input type="radio" name="q2" value="a">option1
<Input type="radio" name="q2" value="b">option2

At the end of the form is a submit button that refers to the GiveResults() function, and the said function is, in its test shape:

function giveresults()
{
zzz=document.testform.q1.value;
document.write(zzz);
}

And then I get the undefined, no matter what.

What am I supposed to do? I can't pass answers from 101 questions as one argument string for a function.

I need to make [if] function for each selectable option since it's a test. In fact, I could even choose something in the shape of:

If document.form.option.selected
{variable1++;
variable2--;
variable3++;
variable4--;}

And then everything selectable would have its own name, rather than one name for a group of options each inflicting a different value adjustment when selected.

But how does one frame it properly in Java Script? Or maybe you know a better way to deal with the wretched form?

It has to be in JavaScript because nothing else is supported by my host. No CGI, no PHP etc, just JavaScript or VB - and I'd rather avoid VB.

Special: I need it to update each time someone changes an object's value and it can't perform ++ and -- on my variable again each time someone changes his mind back and forth.

The only way I see now is for each option to have an onclick function setting a 0/1 variable and then after clicking the submit button, it would have to go by an if function for each 1. That's doable but quite absurd. Mad Shocked

Any suggestions? Thanks very much in advance for any help.
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 2 • Views: 7,698 • Replies: 10
No top replies

 
Craven de Kere
 
  1  
Reply Sat 22 Nov, 2003 06:33 pm
I don't think it's absurd. Please post the full script and I'll try to have a look at it.
0 Replies
 
chevalier
 
  1  
Reply Sun 23 Nov, 2003 05:05 am
There's not much scripting done and written down as I haven't been able to get the variables right from the form - and I can't see why. AFAIK, that should work. But then, that would work if it were right, so I'm missing something.

It's on [url]chevalier.szm.com/rptest.html[/url]

The form is done and at the end of the source there's that little script which tries to extract a variable and 'undefined' is returned.
0 Replies
 
Monger
 
  1  
Reply Sun 23 Nov, 2003 05:48 am
Hi chevalier,

Find this in your code:
Code:document.write (document.testform.q1.value);

Replace it with:
Code:document.write (document.testform.q1[some_number_goes_here].value);


To find out which option is selected, you could write a script something like:
Code:var found;

for (var i=0; i<document.testform.q1.length; i++) {
if (document.testform.q1[i].checked) {
found = document.testform.q1[i].value
}
}

Then use the variable:
Code:document.write(found);

Wink
0 Replies
 
Monger
 
  1  
Reply Sun 23 Nov, 2003 05:54 am
One other thing, you should prolly move your function code from the end on the page underneath BODY to somewhere within HEAD at the top. That's where it should be at, so I dunno...some browsers might choke on its current placement.
0 Replies
 
chevalier
 
  1  
Reply Sun 23 Nov, 2003 06:02 am
Yea, the offline version I was tampering with had it moved properly to the head section.

Thankies Very Happy

/me opens evil source for the Nth time
0 Replies
 
Craven de Kere
 
  1  
Reply Sun 23 Nov, 2003 06:03 am
What you are missing is that the radio button is checked or not checked.

Your method will work for other form field types like a text input but for radio buttons you will need to do some scripting.

I was about to do it but Monger seems to have done so, he even made the next suggestion I had about moving the script above the form.

BTW, I never got an answer from ya about the RPG project. Lemme know how that's going.
0 Replies
 
chevalier
 
  1  
Reply Sun 23 Nov, 2003 06:17 am
Aww, indeed. It has to have the index in square brackets. Right, I treated radio button like it were a combo box. And it starts with 0, not 1. Eh, I'm not so bad for a whacked humanist, aren't I?

My needs, apparently, will be met by:

if (document.testform.q1[4].checked) {
document.write(document.testform.q1[4].value) }

Where document.write will be substituted by weird maths until it's all working and giving a more or less realistic score. I just hope the output html file won't exceed 200KB Wink

Thanks, guys!
0 Replies
 
Monger
 
  1  
Reply Sun 23 Nov, 2003 06:33 am
Embarrassed Sorry, stupid mistake on my part. . .

In my script above, replace this:
document.write(document.testform.q1[found].value);

With this:
document.write(found);

Should have tested it before posting I guess. I'll go ahead & make the change to my post above.
0 Replies
 
Monger
 
  1  
Reply Sun 23 Nov, 2003 06:36 am
One thing though, that script will still return undefined if no radio button is selected. You can just use an [if] to check for that then assign some other value to the found variable, or step around the document.write()
0 Replies
 
Craven de Kere
 
  1  
Reply Sun 23 Nov, 2003 07:20 am
Or just define a default "checked" one.
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » Multiple choice quiz in Java Script
Copyright © 2025 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 01/28/2025 at 07:11:52