Click to See Complete Forum and Search --> : radio buttons and values


Ozzie
07-20-2003, 03:33 PM
Hi

I am very new to this and would love some help to manipulate the values of a selected radio button in a quiz I have worked on.

I have a quiz of 10 questions. I then email the results of the quiz to myself. See code below. I woul like to be able to score each answer and add that value t the bottom of the email.

Can anyone help, please?

<script language="JavaScript">
function onSubmit(form)
{
}

</script>

</head>
<body bgcolor="#FFFFFF" text="#000000">

<FORM enctype="text/plain" name="form" method="post" action='mailto:me@myemail.net?subject=Test Results' >

<br>
<div align="left"></div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<h2>Sample Test</h2>
<tr>
<td bgcolor="#FFFFFF" valign="top" align="left" colspan="3" height="5034">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" align="left" height="5010">
<table width="100%">
<tr valign=top>
<td height="4" class="questions" width="22">&nbsp;</td>
<td height="4" class="questions" width="310">
Please type your name in the text box below.<br>
<br>
<input type="Text" name="fullname" size="60">
<br>
</td>
<td colspan="2" height="4" class="tions">&nbsp;</td>
</tr>
<tr valign=top>
<td height="70" class="tions" width="22">1.</td>
<td colspan="3" height="70" class="tions">What is the shortcut
command for shutting down the computer?<br>
<br>
<input type="radio" name="1" value="a">
Alt + X<br>
<input type="radio" name="1" value="b">
Ctrl + Alt + Delete<br>
<input type="radio" name="1" value="c">
Ctrl + Esc<br>
<input type="radio" name="1" value="d">
Ctrl + S</td>
</tr>
<tr valign=top>
<td height="67" class="tions" width="22">2.</td>
<td colspan="3" height="67" class="tions">If you have typed
a 5-page document, but only want to print the first two pages.
What printer command should you select? <br>
<br>
<input type="radio" name="2" value="a">
print all <br>
<input type="radio" name="2" value="b">
from ___ to ___<br>
<input type="radio" name="2" value="c">
page setup<br>
<input type="radio" name="2" value="d">
print preview</td>
</tr>
<tr valign=top>
<td height="2" width="22" class="tions">&nbsp;</td>
<td colspan="3" height="2" class="tions">&nbsp;</td>
</tr>
<tr valign=top>
<td height="11" width="22" class="tions">3.</td>
<td colspan="3" height="11" class="tions">Which of the following
describes the physical components of a computer, such as the
monitor, keyboard, and storage devices? <br>
<br>
<input type="radio" name="3" value="a">
software<br>
<input type="radio" name="3" value="b">
shareware<br>
<input type="radio" name="3" value="c">
hardware<br>
<input type="radio" name="3" value="d">
programs </td>
</tr>
<br>
<td valign="middle" align="center" height="25" colspan="3">
<p><br>

<input type= submit value="Submit Test">
&nbsp;&nbsp; <br>
<br>
</p>
</td>

</tr>
</table>

</form>


</body>
</html>

David Harrison
07-20-2003, 05:21 PM
Javascript considers radio buttons as an array, therefore you don't really need the value="a", value="b" etc. You can find out using a loop which one is selected and that would be in the form of a number rather than a letter.

What you have to remember though is that the first item in an array is numbered as 0, and the second as 1. So to find which radio button is selected have this loop for each question:

for(var n=0;n<document.fullname.1.length;n++){
if(document.fullname.1[n].checked){break;}}

var ans_to_q1=n+1; // The +1 here is so that if the first radio button is selected it is not 0, but 1, and for the second 2 etc.

Have this for each question, obviously you will have to change the name of the variable ans_to_q1, and also remember this:

document.fullname refers to the form
document.fullname.1 refers to the first group of radio buttons (the 1 is the name of the group, if you were to call the group horses it would become document.fullname.horses)

The script has the potential to become very complicated, especially when there are 10 questions and if you are just starting to learn js.

You can use a series of if statements to determine if the answers are correct or not:

var score=0;

if(ans_to_q1=2){score++;}

etc.

Although it may not be wise to include all of the answers in tha quiz because some people with a little knowledge could find them and cheat.

If you would like I could write a working script like this for tomorrow with just a couple of questions so that you could modify it.

Ozzie
07-20-2003, 06:18 PM
You are wonderful to offer to write a small script for me to modify and I will happily accept.

You are the best. :cool:

David Harrison
07-21-2003, 02:50 PM
OK, this script will total up the score and e-mail it along with the form if javascript is enabled.
If it isn't it will email the form and will display the score as Javascript Not Enabled, so then you will have to mark it as usual (this will only happen about 13% of the time).

I don't think it works with Outlook Express though.

Ozzie
07-21-2003, 02:55 PM
I just picked up your script and will read through and try and learn from it. Thank you very much, you have made my day....
Have a good one and thank you again.

David Harrison
07-21-2003, 03:01 PM
You're too kind, but don't let that stop you. :)

Erm, I've just noticed that in the script there's an absolutely useless bit of code that was used when I was part way through making it, but later on I found a better way of doing it. I just thought I'd mention this so that it doesn't baffle you later as to what it's used for. The useless bit is this:

var answers=new Array();
answers[0]=3;
answers[1]=2;
answers[2]=4;
answers[3]=1;

so feel free to delete it.

Ozzie
07-21-2003, 03:12 PM
OK, I see that part just under the first function

If I wanted to assign a score of more than one to each question, for example each question was worth 3 points, would I modifiy the score statements to read as follows:

score+=(q1check==3)?3:0;
score+=(q2check==2)?3:0;
score+=(q3check==4)?3:0;
score+=(q4check==1)?3:0;

:)

David Harrison
07-21-2003, 03:15 PM
Yep, do exactly that. But if all of the questions are worth three points (for some reason), you could just do:
score=score*3

at the end.

Ozzie
07-21-2003, 03:38 PM
Thanks for your patience...
I think I am doing something wrong though
when I run your script
I answer all 4 questions
confirm that I have finished
my Outlook opens with the correct address and subject
but
this is the only thing that appears in the body of the email.

?q1=Paris


Have I missed something?
:confused:

David Harrison
07-21-2003, 03:45 PM
Well I did say that it may not work with Outlook Express. Although I would have thought that you would have realised this when you tried the script that you posted.

What you could do is post in one of the server side script forums (ASP, CGI, PHP etc.) and ask for a script that would e-mail the answers from the server rather than the users PC.

Or you could wait for a week or so, because I'm gonna start learning ASP soon.

Ozzie
07-21-2003, 03:49 PM
you are very good so good luck with the asp

I am not using Outlook Express...
My original script emails the answers through Outlook without a problem

I am going to try it again later on a different PC. :D

David Harrison
07-21-2003, 03:58 PM
That's interesting because the form is essentially the same as the one you made. I'll have to investigate this further.

Well if I get it to work I'll let you know.

Ozzie
07-21-2003, 04:02 PM
Thank you for everything
and yes
I would love to hear from you if you get it working

If it works on a different PC I will also let you know...

Have a great night, lavalamp

David Harrison
07-21-2003, 04:08 PM
OK bye for now, happy to help, even if was just a little. :)