HTML Applications TutorialHTML Applications Tutorial #5

I've decided to cover loops another time and give you something a little more interesting. WEBlications can do much more than display with message boxes. They can use HTML to display text boxes, buttons and all the other things that Web pages can. We'll start with a simple Hello World program but using some HTML GUI widgets to make it all look a lot more interesting. Here's the script code first.

Option Explicit
Dim strName

Sub check_name
strName = txtName.value
if strName <> "" then
msgbox "Hello " & txtName.value
else
msgbox "Please enter a name"
end if
end sub

Looks fairly innocuous except that on it's own it doesn't work. That's because it's expecting there to be a text field named txtName to be present. The subroutine itself needs to be called in response to a button being pressed. The HTML you need, which goes between the body tags in your template file, is as follows:-

Please enter your name<P>
<INPUT TYPE="Submit" ID="btnGo" VALUE="Go" onclick=check_name()>
<INPUT TYPE="Text" ID="txtName" size="22"><BR>

I'll go throught the HTML first.

Please enter your name<P>

Some helpful text asking for a name to be entered.

<INPUT TYPE="Submit" ID="btnGo" VALUE="Go" onclick=check_name()>

Then below that there is a button defined with the name btnGo. The bit to watch is the onclick which is an event triggered by the user clicking on the button. The event then triggers a call to the subroutine in the script called check_name. Just think about that for the moment. Even though this is HTML, it can contain a little bit of script in it's tags that can call a routine between the script tags.

<INPUT TYPE="Text" ID="txtName" size="22"><BR>

Finally, we have the text box that the script above is expecting. It has been set to a size (width) of 22 characters although it can contain much more tahn 22 characters because it scrolls along. It is up to the script writer to ensure that the input string isn't to large and prompt the user to change it. More of that another time. That's the HTML. Now for the script.

Option Explicit

Ensure that all variables are created before use.

Dim strName

Set a variable to contain a string.

Sub check_name

Start of the subroutine called from the HTML event onclick.

strName = txtName.value

The string variable we created will now take the value of whatever is in the text box. Remember, the user should have put a name in the box then pressed the Go button which triggered this routine to start.

if strName <> "" then

But we can't trust the user not to have pressed Go before putting in a name so we see if the text box is still empty.

msgbox "Hello " & txtName.value

If it isn't (<> means not equal to) then show the name in a message box along with a nice peice of greeting text.

else

otherwise

msgbox "Please enter a name"

Ask the user nicely for a name to be entered.

end if

End the if statement.

end sub

Finish the routine.

This gives a very brief insight into how HTML content can be displayed and controlled by a script. More next time.

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk if you need help