Book HomeActionScript: The Definitive GuideSearch this book

18.2. User-Input Text Fields

User-input text fields differ from dynamic text fields only in that they may be edited by the user while the movie is playing. That is, a user can type into a user-input text field to change its value. ActionScript can then retrieve and manipulate the user-entered value. User-input text fields are useful for guest books, order forms, password-entry fields, or anywhere you request information from the user.

18.2.1. Creating a User-Input Text Field

To create a user-input text field, follow the same steps described earlier under Section 18.1.1, "Creating a Dynamic Text Field", but choose Input Text instead of Dynamic Text from the Text Type menu.

18.2.2. Changing the Content of an Input Text Field

Like dynamic text fields, user-input text fields may be changed at any time simply by setting the value of the named text field with an assignment statement:

myInputText = "Type your name here";

Because user-input text fields are normally used to accept data rather than display data, we don't usually set their contents except to provide a default value for the user's input.

18.2.3. Retrieving and Using the Value of an Input Text Field

You can retrieve the value of a text field by simply referring to it by name in a script. For example, to display the value of an input text field called myInput, use:

trace(myInput);

Because data entered by the user into a user-input text field is always a string datatype, we should convert it explicitly before using it in a non-string context, as demonstrated in this simple calculator example that totals two user-input text fields:

// Suppose the user sets myFirstInput to 5 and mySecondInput to 10,
then we total the fields
// WRONG: "Adding" the fields together sets myOutput to "Total: 510" 
// because the + operator is interpreted as a string concatenator
myOutput = "Total: " + (myFirstInput + mySecondInput);
// RIGHT: Convert the fields to numbers first in order to get the right result
myOutput = "Total: " + (parseFloat(myFirstInput) + parseFloat(mySecondInput));

18.2.4. User-Input Text Fields and Forms

User-input text fields are often used for fill-in forms submitted to a server-side application such as a Perl script. When variables are submitted to a server via loadVariables( ), only the variables defined in the current movie clip are sent. Hence, when a form contains user-input text fields, the fields should be stored in a single, separate movie clip so that they can be submitted easily as a group to a server. See Chapter 17, "Flash Forms", and Part III, "Language Reference" for additional details on loadVariables( ).



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.