Recipe 8.7. Retrieving User Input


Problem

You want to retrieve the text that a user has entered into a text field, text input, or text area instance for further processing.

Solution

Use ActionScript to access the value by way of the instance's text property.

Discussion

You must use ActionScript in order to retrieve the value from an input text field, text input, or text area. However, the code that is required is short and simple. You can access the text value using the instance name and its text property.

This following code writes the value of an input text field to the Output window when you are testing your movie. The text field in this example has an instance name of tInputField. In order for this code to work, it must be placed on a frame in the same timeline in which the text field exists:

 trace (tInputField.text); 

Text that you retrieve from an input text field, text input, or text area is always a string value. The string data type can perform differently in some operations than a number data type. For example, when you use the plus sign with two numbers, they are added together. But when you use the plus sign with two strings, the values are appended to one another. Therefore, if you want to perform mathematical operations on a value that you retrieve from an input text field, you should convert that value to a number data type. You can do this very quickly using either the parseInt( ) or parseFloat( ) functions.

The parseInt( ) function takes a string value, and returns an integer (a whole number) if possible. The parseFloat( ) function takes a string value, and returns a floating point number (a number with a decimal place) if possible. Here is an example of how you can use these functions to convert text field values to numbers:

 var nFloat:Number = parseFloat(tInputText.text); var nInteger:Number = parseInt(tInputField.text); 

Typically you'll retrieve the value from a text field, text input, or text area only after the user clicks a button or otherwise signifies that she has entered the information and wants Flash to do something with it. If you retrieve the information before that, you're likely to retrieve empty values. That means that the code that you use to handle the text should be placed within a method or function that gets called once the user has clicked the button, pressed a key, and so on. Refer to Chapter 9 for more information on handling button events. However, the following is a simple example of some code that displays the user's input in the Output panel after she clicks the button with an instance name of btSubmit. The Output panel is only available within the test player, and therefore the example is only relevant for demonstration purposes. A more practical example requires slightly more sophisticated code. You can find several relevant, related examples in Chapter 13.

 btSubmit.onRelease = function():Void {   trace(tUsername.text); }; 




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net