Next: The Server (CGI) Side Up: Implementation of the Tutorial Previous: Implementation of the Tutorial

Coding the Question Forms

Coding the form for a short answer question is quite easy using a text box. For example, the form shown below is used in Part 5-7 which discusses some of the string functions in the standard library.

<hr>
Write an <code>if</code> statement (just the
first line, not the body) that tests to see if
the string named <code>target</code> is equal
to the name of student number n in the array:
<pre>
char names[1400][30] ;
</pre>
<form action="http:proc5-7.cgi"
  method="post">
<input type="text" size=40
  name="answer1" value="">
<input type="submit"
  value="Submit Answer">
<input type="reset"
  value="Clear Answer">
</form>
<hr>
This fragment of HTML code is the description of the form which is seen in Figure 2. Here we have three input fields in the form, a text box, a submit button and a reset button. The reset button just clears the text window. The submit button is used to send the student's answer back to the tutorial server. Text input boxes are named and can have an initial value. Here we've named the box answer1 and have given it no initial value. When the submit button is clicked, a message is sent from the browser to the server asking that the proc5-7.cgi program be run with an input string containing the current contents of the text box labeled by its name.

In addition to the text input type, HTML specifies a radio button input type. With this input type, the browser automatically gives the correct one-of-many behavior in the user interface. The following example illustrates its use.

<hr>
What will this loop print?
<pre>
for( i = 0; i < num_stud; ++i ) {
  if( grades[i][3] > 90 && names[i][1] == 's' )
     printf( "%s\n", names[i] ) ;
}
</pre>
<form action="http:proc5-5.cgi" method="post">
<input type="radio" name="answer5-5" value="A">
All names of students whose names have the
first letter s or who scored better than 90 on
the third assignment.<br>
<input type="radio" name="answer5-5" value="B">
All names of students whose names have the
second letter s or who scored better than 90
on the fourth assignment.<br>
<input type="radio" name="answer5-5" value="C">
All names of students whose names have the
first letter s and who scored better than 90
on the third assignment.<br>
<input type="radio" name="answer5-5" value="D">
The name of the first student whose name has
the second letter s and who scored better than
90 on the fourth assignment.<br>
<input type="radio" name="answer5-5" value="E">
All names of students whose names have the
second letter s and who scored better than 90
on the fourth assignment.<br>
<input type="submit" value="Submit Answer">
<input type="reset" value="Clear Answer">
</form>
<hr>

Here all the radio input devices have the same name. This tells the browser that they are to be grouped into a single radio group where if one is pressed, all others are released. When the form is submitted, the browser sends a message of the form answer5-5=D (here, we've assumed that the fourth option was selected).

A many-of-many multiple choice question can be implemented by using checkboxes. These boxes are not connected like the radio boxes are. Here each box has a different name, but they all will have the value T, if they are checked. This is illustrated in the example below.

<hr>
Which statements are valid if
<code>name</code> is an array of characters.
<form action="http:proc5-4.cgi" method="post">
<input type="checkbox" name="answer5-4a"
  value="T">scanf( "%s", name ) ;<br>
<input type="checkbox" name="answer5-4b"
  value="T">scanf( "%s", &name ) ;<br>
<input type="checkbox" name="answer5-4c"
  value="T">name[5] = 'a' ;<br>
<input type="checkbox" name="answer5-4d"
  value="T">name[5] = '\n' ;<br>
<input type="checkbox" name="answer5-4e"
  value="T">name = "Me" ;<br>
<input type="submit" value="Submit Answer">
<input type="reset" value="Clear Answer">
</form>
<hr>


Next: The Server (CGI) Side Up: Implementation of the Tutorial Previous: Implementation of the Tutorial

Brian L. Stuart
Mon Aug 12 16:20:19 CDT 1996