Tcl Powered!NeoWebScript

Home
 
Download
User Info
New User FAQ
Tutorials
Demos
Commands
Variables
Troubleshooting
Sysop Info
Sysop FAQ
Theory
Installation
Management
Tests
Troubleshooting
Feedback
Resources
Release Notes
Credits
Disclaimer

Tutorial #2

The World Talking Back...

In this tutorial, we will cover the use of forms, getting information from the user, and processing that information. We will also see how to store information, send it out in the form of e-mail, and how to write simple procedures.

All about forms

Hopefully by now, you've read the first tutorial and have some grasp as to using the environment, using the html command, and using variables and commands. You should take a look at the other variables available to you--after all, there are tons of stuff you can use! The pages we can write now are pretty much "self-contained." Wouldn't it be great if we could interact with the user? Well, we can--through the use of forms.

Below is a simple form:

Name:
Age:
Submit:

I'm sure that you've seen forms like this on the Internet. The code used to make the form looks like this:

<form method=post action=tutorial2.nhtml>
Name:  <input type=text name=name><br>
Age:  <input type=text name=age><br>
Submit:  <input type=submit>
</form>

Let's take a look each part of the form:

  • <form - introduces the form section
  • method=post - says that we are going to use the post method to send the results. Some browsers don't support POST. The other option is GET.
  • action=tutorial2.nhtml - Where should these results go? In this case, right back to ourselves.
  • <input - An element in the form, in this case the input tag
  • type=text - We just want regular text from them
  • name=name - The name of this particular field is "name"
  • > - We have to close our input tag.
  • </form> - We also have to close the form tag.

Putting forms to work for you

Once you submit a form, NeoWebScript takes care of all the details for you. Toward the beginning of your NeoWebScript snippet, simply call the command load_response, which pulls in any information available into an array called response. Remember the name=blah tags in the above example? Well, they're back--in the form of the "response" array.

Let's make this more concrete. Let's say somebody filled out the above form, and we want to process what they said. Here's the code to do it:

<nws>
load_response

html "Hello, $response(name)."
html "You must be $response(age) "
html "years old today."
</nws>

The above little snippet produces the following:

Fill out the form at the top of the page and hit "submit"

Another Example

Let's take another look at processing data received from forms. The basic idea is this:

  1. Write a form using the regular HTML tags.
  2. Make sure the action tag points to a page with NeoWebScript.
  3. In that NeoWebScript-enabled page, call the load_response command.
  4. WOW! You now have an array that contains all the information the user entered.
Let's put this information to good use by creating an order form for a music store. We'll need two things: the form, and the NeoWebScript page that will process the output of the form. We'll start with the form.

*Note: Click HERE to go through the process of ordering once. After that, you will be able to better see how the form and form action methods work.

Taking a closer look at processing forms

Hopefully by now you've had a chance to look through the little music form above, and want to make something like it. Let's take a look at each of the steps involved:

Step 1 - Creating The Form

The first thing you need to do is determine exactly what information you want from the user, and what you want to do with it. For example, if you are asking for a person's name, a text input is probably the best bet. On the other hand, if you are asking for a mailing address, then maybe the textarea tag would be better suited, because addresses can not always be fit into pre-defined fields, and other countries do not always have the same addressing schemes as the United States. Remember: people could visit you page from anywhere in the world with the simple click of a mouse.

Now that you have an idea of what type of information you want from the user, you need to find a way to get what they filled out into your action page. The action page is simply the reciever of the information. Using HTML form tags, you would specify something like this:

<form action=my_action_file.nhtml method=post>
...
</form>

In this case, all of the information will be sent to "my_action_file.nhtml". The "method=post" tag simply says to send the information using a method that most modern browsers can understand. If you are concerned with older versions of software visiting your web page, you could try:

<form action=my_action_file.nhtml method=get>
...
</form>

In either case, once you get your form setup the way you like it, you should save it as a text file, and make sure the extension is ".nhtml".

Step 2 - Writing up the receiving end

Now your form looks pretty, and you can fill it out and submit it, but--OOPS!-- fumble! The reciever wasn't there to catch your data. It looks like you'd better create the "my_action_file.nhtml" file.

Getting "my_action_file.nhtml" to accept your data is pretty simple. All you have to do is insert the load_response tag somewhere in a NeoWebScript snippet, and the webserver will take care of the rest. Mind you--it doesn't do much right now, but we can address that in a little while. Take a look at the music example above, and see how the load_response command takes the data they entered and puts it into the response array. Remember our discussion of arrays in Tutorial 1? Well, it's no different here. The only difference is that the user provided the data, not us.

What now?

Well, you've got a working form, and you have a page that loads the values a user provides into an array. Let's see what that looks like up to this point:

<nws>
load_response
</nws>

Let's store this information into a database. WHOA!!! Hold on... Did I say a database? Well, I did. Before you despair, realize that with NeoWebScript, storing data into a database is really easy. For sure a lot easier than you would think. In fact, all it takes is one line of code.

Let's see what it would look like if we stored the information from our "musical" form into a simple database:

<nws>
load_response

dbstore myDatabase myKey response
</nws>

Let's take a look at each step in the process:
  • dbstore - This is the name of a command that will store our data for us.
  • myDatabase - The name of the database we want the information stored in.
  • myKey - This is what we'll store the information as. It's much like the tabs on a rolodex. Although we may have a lot of information about several people, we will only file that information under the person's name. The key is the index that we use to find our information. We'll talk more about keys in the next section.
  • response - The name of the array to store. It will store anything inside that array for us.
Please realize that the names given above can really be anything. For example, instead of "myDatabase", I could have called it "my_database", "bob", or "abc123". To further explore the example above, we see that the information they sent is stored into the database "myDatabase", and filed under the key "myKey". You may be thinking, "Well, won't the next entry clobber the current one?" It will--which is why you normally want to pick a key unique to that response. For example, we might want to store an array using the person's last name as a key, or perhaps using their phone number. That way, when we "fetch" a record, we can enter their phone number and get all the information about them. (Ever wonder why some stores ask you for your phone number when you try to buy something?)

You keep the key

The best way to store information in a database is to store it under something unique to that entry. To do that, we choose a key that is unique. In the above example, we could store the record under the person's name, which would be pretty unique. That way, when we want to lookup what they sent us, we can just type use thier name to get our information. Let's pretend that the user filled in a form, and that one of the items in the form is a phone number. We decide to store the information based on that phone number:

<nws>
load_response

dbstore musicDB $response(phonenum) response
</nws>

Do you see how the key that we are storing the information under is unique to that request? To get the information back, it's just as easy:

<nws>
dbfetch musicDB 7135551234 customer

html "Hello, $customer(name)!  Welcome back."
html "You last purchased a $customer(lastitem)."
html "Enjoy!"
</nws>

What would that produce? Assuming that we had an entry for them, it would emit the following:

Hello, Hector!  Welcome back.
You last purchased a Spear.
Enjoy!

Let's take a more indepth look at the dbfetch command:

  • dbfetch - the name of the command that "gets" information for us
  • musicDB - the name of the database to go for
  • 7135551234 - In this case, the key to look under for the information.
  • customer - NeoWebScript will use this as the name of an array to create to hold the information.

So where does that leave me?

With the information presented above, you should be able to handle some simple NeoWebScript code. In the first tutorial, we learned about getting to the outside world, emitting HTML, and using variables and commands. In this tutorial, we learned a bit about getting information from the user by means of forms. The will be future tutorials concerning logging, user tracking, and counters.

Happy Scriptin'

Copyright © 1995-1999 NeoSoft Inc., 1770 St. James Place, Suite 500, Houston, TX 77056 USA. All Rights Reserved