The Computer Gal Logo - Laptop with coffee and plants
Collecting User Entry from a Form

The most common way to get data from a web viewer is with a form. A form has three main parts to design and build before the programming is even started, 1) the form tag, 2) the form fields and their labels, 3) the submit button. Our first example will by-pass the need for a button.

Here is an example of a simple form in a table:

Enter a number
Enter another number
The product of your numbers is:
Background Information
  1. One of the properties of the <form> tag is action="". The action is what happens after someone has clicked a Submit button. In our example, we don't have a Submit button; so, we can't call an action. Most often, information on a form goes to some program on the server for processing. This is true in server-side programming languages. In our case, we want the JavaScript to show us the results of what we had entered, right here on this page. That is client-side programming.
  2. To understand programming, we have to understand what order things have to be done. With JavaScript, we have the problem of how to get information from the user and then put it back on the screen.
  3. If we collect user entry from a prompt box that pops up before the page loads, the page sits there and waits until the user enters something. Then browser is able to use that information as it creates the page because it is available before the page loads. You can check this out in the Output Exercise. However, with a form, the page is already created, and it's not so simple to add something to it with JavaScript, unless the browser can rewrite the page.
  4. We could put the information back to the screen in an Alert box or send it to another web page, which hasn't loaded yet. Another active element on your web page, however is a form element. We could have JavaScript repeat the information back into textboxes.

Exercise Instructions

  1. Create a form in a table with four rows and two columns. If you need help building a form, click here.

To gather this information and multiply it, we will use a function. A function is a set of instructions that is part of a computer program. A function is set up like this:

<script language="JavaScript">

function multiply()
{
    var first = document.SimpleExample.First.value;
    var second = document.SimpleExample.Second.value;
    window.document.SimpleExample.Product.value = first * second;
}

</script>

Now you have the function written, but the form doesn't know that it's there. You have to tell the form to run the script. You don't want the script to run until the user has entered some numbers. There are many ways to call a function.

    1. The form action property
    2. The Submit button
    3. In the individual form elements.

In this case, we will use the third method. We are ready for an answer as soon as someone has typed their second number. We can use the onChange property to have the answer pop up as soon as they leave the second textbox.

Add onChange="multiply()" to the second form element.

Notes about the code:

  •  It is most common to place functions at the top of the code in the <head>.
  •  Again, you have to tell the browser to call the JavaScript interpreter, but you can put many functions in the same <script></script>

Related Articles

  1. Using a Text Editor and a Browser to Create a Web Page
  2. What is JavaScript?
  3. Using a Pre-defined Function in JavaScript
  4. Variables, Math, Concatenation and Overloaded Operators
  5. Using a Prompt Box and Outputting to the Screen
  6. Basics about Forms
  7. Adding JavaScript to a Form

Nora McDougall-Collins | Missoula, Montana 59801 | 406.253.4045 | info@thecomputergal.com
© 2009, Nora McDougall
Nora McDougall-Collins | Missoula, Montana 59801 | 406.253.4045 | info@thecomputergal.com
© 2009, Nora McDougall