Obtaining Information from Forms

 

Introduction.  So far we have enacted fairly artificial interactions with forms.  One of the major things we need to be able to do is access the information a user inputs into a form within some sort of processing scripts.

At this point we will develop a relatively simple example that makes use of our registration form and then displays the information the user enters on another page.  The following code is probably the most complicated we have seen so far.  We will discuss it after you implement it and we will improve on it later in the semester in at least a couple ways.  I have highlighted the comments used to compartmentalize the sections of code that actually read in the values from the form for each type of input control.  You should copy this code into a file called ReadFormInfo.html.

 

<html>

<head>
<title>Registration Info</title>

<script language="JavaScript">

function DisplayUserInfo()
{
// code to read from text boxes
var Username = window.document.RegistrationInfo.txtUsername.value;
var Password = window.document.RegistrationInfo.txtPassword.value;

// code to determine which Sex was chosen in the drop down
// make sure you set the value in each option to get this to work

var SexIndex = window.document.RegistrationInfo.cboSex.selectedIndex;
var Sex = window.document.RegistrationInfo.cboSex.options[SexIndex].value;

// code to determine if each check box was checked
var Programming = window.document.RegistrationInfo.chkProgramming.checked;
var AnalysisDesign = window.document.RegistrationInfo.chkAnalysisDesign.checked;
var ISManagement = window.document.RegistrationInfo.chkManagement.checked;
var SysAd = window.document.RegistrationInfo.chkSysAd.checked;

// code to determine which radio button was selected
// to be done more efficiently in the future

if (window.document.RegistrationInfo.rbCreditCard[0].checked == true) {

var CreditCard = window.document.RegistrationInfo.rbCreditCard[0].value; }

if (window.document.RegistrationInfo.rbCreditCard[1].checked == true) {

var CreditCard = window.document.RegistrationInfo.rbCreditCard[1].value; }

if (window.document.RegistrationInfo.rbCreditCard[2].checked == true) {

var CreditCard = window.document.RegistrationInfo.rbCreditCard[2].value; }

// code to read from textarea
var SelfRevelation = window.document.RegistrationInfo.tareaDescribe.value;

// code to display the results

window.document.write("<P><font size=4>Your username is " + Username + "</font></P>");
window.document.write("<P><font size=4>Your password is</font><font size=6><b> " + Password + "</b></font></P>");
window.document.write("<P><font size=4>You claim your sexual preference is " + Sex + "</font></P>");
window.document.write("<BR><font size=4>Your interest in programming is always " + Programming + "</font>");
window.document.write("<BR><font size=4>Your interest in analysis and design is " + AnalysisDesign + "</font>");
window.document.write("<BR><font size=4>Your interest in IS Management is " + ISManagement + "</font>");
window.document.write("<BR><font size=4>Your interest in System Administration is " + SysAd + "</font>");
window.document.write("<P><font size=4>The credit card you will use to pay is " + CreditCard + "</font></P>");
window.document.write("<P><font size=4>You describe yourself as " + SelfRevelation + "</font></P>");

}

</script>
</head>

<body bgcolor="#330033" text="#C0C0C0" style="font-family: Lucida Sans Unicode">

<p><font size="5"><b>Please Fill Out the Following Information</b></font></p>
<p>&nbsp;</p>
<form name=RegistrationInfo>
<div align="center">
<table border="0" cellpadding="6" cellspacing="0" width="680">
<tr>
<td align="right" width="339">
<p align="right"><font size="4">Username:</font></td>
<center>
<td width="313"><input type="text" name="txtUsername" size="25" style="font-size: 14pt; font-family: Lucida Sans Unicode"></td>
</tr>
<tr>
<td align="right" width="339"><font size="4">Password:</font></td>
<td width="313"><input type="password" name="txtPassword" size="25" style="font-size: 14pt; font-family: Lucida Sans Unicode"></td>
</tr>
<tr>
<td align="right" width="339"><font size="4">Sex:</font></td>
<td width="313"><select size="1" name="cboSex" style="font-family: Lucida Sans Unicode; font-size: 14pt">
<option value="Female">Female</option>
<option value="Male">Male</option>
<option value="Other">Other</option>
<option value="Never Enough">Never Enough</option>
</select></td>
</tr>
<tr>
<td align="right" width="339">&nbsp; </td>
<td width="313"></td>
</tr>
<tr>
<td align="right" width="339"><font size="4">Please check all
interests:</font></td>
<td width="313"></td>
</tr>
<tr>
<td align="right" width="339"><font size="3">Programming:</font></td>
<td width="313"><input type="checkbox" name="chkProgramming" value="ON"></td>
</tr>
<tr>
<td align="right" width="339"><font size="3">Analysis &amp; Design:</font></td>
<td width="313"><input type="checkbox" name="chkAnalysisDesign" value="ON"></td>
</tr>
<tr>
<td align="right" width="339"><font size="3">Management of IS:</font></td>
<td width="313"><input type="checkbox" name="chkManagement" value="ON"></td>
</tr>
<tr>
<td align="right" width="339"><font size="3">System Administration:</font></td>
<td width="313"><input type="checkbox" name="chkSysAd" value="ON"></td>
</tr>
<tr>
<td align="right" width="339"><font size="4">&nbsp;</font></td>
<td width="313"></td>
</tr>
<tr>
<td align="right" width="339"><font size="4">Select Credit Card Type:</font></td>
<td width="313"></td>
</tr>
<tr>
<td align="right" width="339">Mastercard:</td>
<td width="313"><input type="radio" value="Mastercard" name="rbCreditCard"></td>
</tr>
<tr>
<td align="right" width="339">Visa:</td>
<td width="313"><input type="radio" value="Visa" name="rbCreditCard"></td>
</tr>
<tr>
<td align="right" width="339">Discover:</td>
<td width="313"><input type="radio" value="Discover" name="rbCreditCard"></td>
</tr>
<tr>
<td align="right" width="339"></td>
<td width="313"></td>
</tr>
<tr>
<td align="right" width="339"><font size="4">Please describe yourself
in 50 words or less:</font></td>
<td width="313"><textarea rows="5" name="tareaDescribe" cols="37" style="font-family: Lucida Sans Unicode; font-size: 14pt"></textarea></td>
</tr>
</table>
</center>
</div>
<p>&nbsp;</p>
<div align="center">
<table border="0">
<tr>
<td align="right"><input type="submit" value="Submit" name="cmdSubmit" style="font-family: Lucida Sans Unicode; font-size: 14pt; font-weight: bold" onClick="DisplayUserInfo();"></td>
<td align="left"><input type="reset" value="Reset" name="cmdReset" style="font-family: Lucida Sans Unicode; font-size: 14pt; font-weight: bold"></td>
</tr>
</table>
</div>

</form>
<p>&nbsp;</p>

</body>

</html>

 

You should actually see something like the following upon first entering the URL, though your inputs should differ.

 

 

After pressing the Submit button you should get a page like the following.

 

 

Now we will discuss the code.

Most of this code in the <Body> is just the Register.html form we developed previously with onClick event code in the Submit button HTML code.  The only other change that was required in the form was to give values to each <option> in the <Select>.

The extensive changes occur in the header where we have developed a new function called DisplayUserInfo( ).  Here we need to get what the user entered into the form and display them.

Textboxes.  The code to obtain the values from the textboxes is essentially

// code to read from text boxes
var Username = window.document.RegistrationInfo.txtUsername.value;
var Password = window.document.RegistrationInfo.txtPassword.value;

where the name of the textbox is used in the container hierarchy to identify its value.

Select/Drop Down Boxes.  This option set up is the most interesting.  You can't just retrieve the value that was checked from the drop down.  You need to

  1. determine which option was selected numbered 0, 1, 2, ... length.

  2. use this value in the options array to get the value

While these statements can be combined and improved on more later, our solution required the following code.

// code to determine which Sex was chosen in the drop down
// make sure you set the value in each option to get this to work

var SexIndex = window.document.RegistrationInfo.cboSex.selectedIndex;
var Sex = window.document.RegistrationInfo.cboSex.options[SexIndex].value;

Checkboxes.  Check boxes are different in that you don't determine their value, you determine whether they are checked.  The following code segment does the job and each assignment statement gives either a true or false to the variable.

// code to determine if each check box was checked
var Programming = window.document.RegistrationInfo.chkProgramming.checked;
var AnalysisDesign = window.document.RegistrationInfo.chkAnalysisDesign.checked;
var ISManagement = window.document.RegistrationInfo.chkManagement.checked;
var SysAd = window.document.RegistrationInfo.chkSysAd.checked;

Radio Buttons.  Radio buttons work somewhat differently than the other controls since they are considered to be in an array.  The array runs from 0 to one fewer than the number of buttons in the collection.  When we get to loops we will clean up this code somewhat, or maybe that will be on the homework.  Again, this is very much like the check boxes since we determine which is checked.  But in this instance only one of the collection can be checked.  The code segment follows.

// code to determine which radio button was selected
// to be done more efficiently in the future

if (window.document.RegistrationInfo.rbCreditCard[0].checked == true) {

var CreditCard = window.document.RegistrationInfo.rbCreditCard[0].value; }

if (window.document.RegistrationInfo.rbCreditCard[1].checked == true) {

var CreditCard = window.document.RegistrationInfo.rbCreditCard[1].value; }

if (window.document.RegistrationInfo.rbCreditCard[2].checked == true) {

var CreditCard = window.document.RegistrationInfo.rbCreditCard[2].value; }

We get the value of the button that is checked if its checked property is true.

Text Areas.  Text areas work essentially the same as text boxes and their information can be retrieved quite directly.  The code follows.

// code to read from textarea
var SelfRevelation = window.document.RegistrationInfo.tareaDescribe.value;

This completes our form retrieval in JavaScript.