IF THEN ELSE

After you have used a form to collect information, there are many things that you can do with that information. One of the most powerful tools in the scripting process is the IF statement.

The IF statement is used like this

IF "my defined circumstances are true" THEN "do this process"
END IF

Open this page for a sample

IF THEN SAMPLE

How this Sample works

The first part of the sample page works just like any other form.

The IfThenSample.asp is a normal form, with one named radio button (remember a radio button will permit you to select one answer from the options available). The Submit button takes you to a page with a Request.Form. It looks like this:

Your favorite HTML editor is <% =Request.Form("MyRadioButton")%>.

This is no different than other form result pages you have seen. The next section of the text is determined by the selection you made on the form. There are a total of five possible ways the page can be displayed. I have edited out the extra text, so the scripting can easily be seen.

<% IF Request.Form("MyRadioButton") = "DreamWeaver" THEN %>
Dreamweaver MX 2004 is the professional BLAH, BLAH, BLAH.
<% END IF %>

<% IF Request.Form("MyRadioButton") = "GoLive" THEN %>
All-new Adobe GoLive CS software BLAH, BLAH, BLAH.
<% END IF %>

<% IF Request.Form("MyRadioButton") = "FrontPage" THEN %>
FrontPage 2003 provides the features, flexibility BLAH, BLAH, BLAH.
<% END IF %>

<% IF Request.Form("MyRadioButton") = "Notepad" THEN %>
Apparently you are too cheap to buy software BLAH, BLAH, BLAH.
<% END IF %>

<% IF Request.Form("MyRadioButton") = "" THEN %>
You didn't select one of the HTML editors.
<% END IF %>

There are several things to note:

1. There are a total of six Request.Form commands all going back to the same form element. There is no limit to the number of times you can request the same form element. If this were not a lesson on forms, I would have used the Request.Form command only once, and would have saved the value to a variable. I would have then used that variable in all the IF statements.

2. The IF conditions are case sensitive. The form value for DreamWeaver was "DreamWeaver" not "dreamweaver".

3. A person could choose to leave the form blank. The last IF statement handles this possibility. A blank value is equal to "".

ELSE

What if there are only two possibilities? The IF statement can handle testing to see if the criteria is true. If the criteria is true, the IF statement kicks in. A second part of the IF statement can be used if the IF statement is NOT true. This is the ELSE. Think of the ELSE as an IF NOT true.

The code looks like this:

IF "my defined circumstances are true" THEN "do this process"

Do this if circumstances are true

ELSE

Do this if circumstances are NOT true

END IF

Suppose you want to password-protect a page. If someone knows the password, show the page, ELSE don't show the page.

>Password Protected Page<

Here is an example of code that uses an ELSE statement to password protect a page. You should be able to look at the code and figure out the password.

<% PagePassword=Request.Form("ThePassword")%>
<% IF PagePassword="EDUC565" THEN %>

<body bgcolor="#FFFFFF">
The contents of this page are secret. You have entered the correct password.
</body>

<% ELSE %>

<body bgcolor="#FFFFFF">
<form name="form1" method="post" action="password.asp">
Enter Password:
<input type="text" name="ThePassword">
<input type="submit" name="Submit" value="Submit">
</form>
</body>

<% END IF %>

This page actually points to itself in the action line of the form. Since the previous page has the form, it checks itself to see what was entered.

Notice that there are two BODY tags in the above example. Normally this is not permitted by the rules of standard HTML. It works here because the IF statement permits only one of the BODY tags to be sent to the browser.

Home - What Is ASP - Simple Scripts - Using Forms - Form Elements - Form Processing - Session Variables - IF THEN ELSE