All source code in ASP/ VbScript Ask a ASP/ VbScript Pro Discussion Forum Categories All jobs in ASP/ VbScript
Quick Search for:  in language:    
most,common,uses,scripting,respond,events,fro
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 281,486. lines
 Jobs: 101. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login

NEW! LEARNING CENTER
Special educational offers, white papers, webcasts, podcasts

  NEW! Download Rational Performance Tester V8 (download now)
  NEW! Download Rational Functional Tester V8 (download now)
  NEW! Download Rational Service Tester for SOA Quality V8 (download now)
  NEW! Teleconference: Quality In Action - Using Rational Quality Manager with Functional, Performance and Web Service Testing Products (download now)
  NEW! Introducing IBM Rational AppScan Developer Edition – easing security testing by non-security professionals (download now)

 

 


Latest postings for ASP/ VbScript.
Click here to see a screenshot of this code!Mess Officer Management System (Sistem Pengurusan Mess Pegawai)
By Mohd Fauzi on 11/23

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

VBScript Tutorial: Chapter 4--Frames and Forms and Elements, Oh My!

Print
Email
 
article
Submitted on: 5/25/2000 4:32:23 PM
By: David M. Carr  
Level: Beginner
User Rating: By 5 Users
Compatibility:VbScript (browser/client side)

Users have accessed this article 22260 times.
 
(About the author)
 
     One of the most common uses of scripting is to respond to events from a form, such as typing text into a text box or checking data when the Submit button is pressed. There are two ways to reference form elements...this chapter tells you how.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

Frames and Forms and Elements, Oh My!

One of the most common uses of scripting is to respond to events from a form, such as typing text into a text box or checking data when the Submit button is pressed. There are two ways to reference form elements. First, you can reference them through the Object Model. Document.Form(0).Elements(1).Value refers to the Value property of the second element in the first form on the page. You can determine the number of forms in the page using Document.Forms.Count, and can determine the number of elements in the first form using Document.Forms(0).Elements.Count.

This is an awkward way of referring to elements. Instead, if you include a Name attribute in the form and element's HTML tags, you can refer to it by that.

<FORM NAME="aForm">
<INPUT TYPE="TEXT" NAME="txtGreet">
</FORM>
This element's Value could be referenced using aForm.txtGreet.Value

Unless you are write exclusively for IE4+, you should always put form elements in a form, and use the NAME attribute for them rather than the ID attribute.

Form elements expose both properties and events. The following example shows this.

Sub txtFred_OnFocus()
    With aForm.txtGreet
        If .Value = "Hello" Then
            .Value = "Goodbye"
        Else
            .Value = "Hello"
        End If
    End With
End Sub

The onFocus event fires whenever the element becomes active for input, such as by clicking it or tabbing to it. With is a keyword which allows the use of relative references. Normally, it would be necessary to use aForm.txtGreet.Value all three times, but it is much simpler to use with. Another way to do this would be to use the Set keyword. It makes a variable into a shortcut to another object. Ex:

Sub txtFred_OnFocus()
    Dim theCtl
    Set theCtl = aForm.txtGreet
        If theCtl.Value = "Hello" Then
            theCtl.Value = "Goodbye"
        Else
            theCtl.Value = "Hello"
        End If
    End With
End Sub

Here is an example of using VBScript to check user form input.


<SCRIPT TYPE="text/vbscript" LANGUAGE="VBScript">
<!--
Function lycosForm_OnSubmit()
    If Len(lycosForm.query.Value) = 0 Then
        lycosForm_OnSubmit = False
        Alert "You must enter a keyword."
        Exit Function
    End If
    If lycosForm.cat.Value = "null" Then
        lycosForm_OnSubmit = False
        Alert "You must choose a search type."
        Exit Function
    End If
End Function
'-->
</SCRIPT>

<form action="http://www.lycos.com/cgi-bin/pursuit" method=GET NAME="lycosForm">
Search
<SELECT NAME="cat">
<OPTION value="null">Please Choose a Search Type</OPTION>
<OPTION value="lycos">The Web</OPTION>
<OPTION value="sounds">Sounds</OPTION>
<OPTION value="graphics">Pictures</OPTION>
<OPTION value="point">TOP 5%</OPTION>
</SELECT> for:<INPUT TYPE="text" NAME="query" VALUE="" SIZE=22>
<input type="submit" value="Go Get It"></form>


And here is the result. A form which searches Lycos, but won't submit unless you've chosen a search type, and typed in a keyword.

<SCRIPT TYPE="text/vbscript" LANGUAGE="VBScript"> <-- Function lycosForm_OnSubmit() If Len(lycosForm.query.Value) = 0 Then lycosForm_OnSubmit = False Alert "You must enter a keyword." Exit Function End If If lycosForm.cat.Value = "null" Then lycosForm_OnSubmit = False Alert "You must choose a search type." Exit Function End If End Function '-->

Search for:

Note: This is a variation of the search form that Lycos makes available for use on other people's pages.


Notice that here, I treated the event as a Function rather than a Sub. Is you want to be able to cancel an event, that is how you do it, by making it a function and then returning either True or False, False canceling the event.

Window.Frames is like Document.Forms, in that it can contain multiple instances that can be referred to by name or index, and has a Count property.  The difference is that while Forms contain Elements, Frames contain a Document object.

Frames are much like windows. The contain a document, and can contain other frames. In fact, in almost all ways, you can treat a frame as if it was a window.

You can refer from one frame to another by using the Window.Parent object. It points at the window above it.

Frames are supported in NN2+ and IE3+, and are part of the HTML4 specification. For scripting, though, floating frames are even more useful than regular frames. The IFRAME tag allows you to make a frame which sits in the middle of a page, like an image. It is supported by IE3+, and is not part of HTML4. They are referred to in the same way as regular frames.

Below is an example of a scripted floating frame.

<--webbot bot="HTMLMarkup" startspan --><--webbot bot="HTMLMarkup" endspan --> <SCRIPT type="text/vbscript" language="VBScript"> <-- Dim SPre Dim EPre SPre1 = "

"
    SPre2 = "
"
    SPre3 = "
"
    SPre4 = "
"
    SPre5 = "
"
    EPre = "<" & Chr(47) & "PRE><" & Chr(47) & "BODY>"
    Sub AnimHelloFrame1
    	Window.fraHello.Document.Write SPre1 & "H" & EPre
    	Window.fraHello.Document.Close	
    	SetTimeOut "AnimHelloFrame2 True", 250, "VBScript"
    End Sub
    Sub AnimHelloFrame2(forw)
    	Window.fraHello.Document.Write SPre2 & "He" & EPre
    	Window.fraHello.Document.Close	
    	If forw Then
    		SetTimeOut "AnimHelloFrame3 True", 250, "VBScript"
    	Else
    		SetTimeOut "AnimHelloFrame1", 250, "VBScript"
    	End If
    End Sub
    Sub AnimHelloFrame3(forw)
    	Window.fraHello.Document.Write SPre3 & "Hel " & EPre
    	Window.fraHello.Document.Close	
    	If forw Then
    		SetTimeOut "AnimHelloFrame4 True", 250, "VBScript"
    	Else
    		SetTimeOut "AnimHelloFrame2 False", 250, "VBScript"
    	End If
    End Sub
    Sub AnimHelloFrame4(forw)
    	Window.fraHello.Document.Write SPre4 & "Hell " & EPre
    	Window.fraHello.Document.Close	
    	If forw Then
    		SetTimeOut "AnimHelloFrame5", 250, "VBScript"
    	Else
    		SetTimeOut "AnimHelloFrame3 False", 250, "VBScript"
    	End If
    End Sub
    Sub AnimHelloFrame5
    	Window.fraHello.Document.Write SPre5 & "Hello" & EPre
    	Window.fraHello.Document.Close	
    	SetTimeOut "AnimHelloFrame4 False", 250, "VBScript"
    End Sub
    Sub Window_OnLoad()
    	AnimHelloFrame1
    End Sub
    -->
      

<--webbot bot="Navigation" S-Type="arrows" S-Orientation="horizontal" S-Rendering="text" B-Include-Home="FALSE" B-Include-Up="TRUE" U-Page S-Target -->
 
<SCRIPT SRC="http://library.thinkquest.org/tq-admin/tqtrailer.js">


Other 3 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this article(in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
11/12/2002 8:52:41 PM

It'a nice !
Give some more ti\utorial in forms!
(If this comment was disrespectful, please report it.)

 
6/2/2003 3:08:32 PM

Believe it or not, this was the first example I found in 3 days of searching that showed the syntax formname.selectname.value and how to use it. Thank you.

Do not reply to this address...I'll never see it. But, thanks again.
(If this comment was disrespectful, please report it.)

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2008 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.