What is Software? | What is Education? | What is Technology? | What is Research? | What is Wi-Fi? | What is Communication? | What is Customer Service?

Free SEO Tutorials & Help

Tutorials:   Adobe Flash   Adobe Photoshop   CorelDRAW   SEO  

Site Map

Website Development | Cable & Broadband Magazine | Solutions for Higher Education | Auction Anything Online

Adobe Flash Help & Tutorials

 Back to Topics

 

Using ActionScript: How to Create a Form with Conditional Logic and Send Data in Flash

ActionScript: Create a Form with Conditional Logic and Send Data

Set up your workspace

Add an input text field to collect form data

Add a Submit button to the form

Move between scenes

Add an error message

Add a confirmation message

Add a stop() action

Add frame labels for navigation

Add conditional logic for the Submit button

Pass data out of a SWF file

Write a function for the Try Again button

Test your SWF file

Summary

ActionScript: Create a Form with Conditional Logic and Send Data

TOP

You can create a form with conditional logic that allows the SWF file to respond to user interaction and send the form data from the SWF file to an external source. This tutorial shows you how to create a simple form.

Before taking this lesson, you should be familiar with writing functions and variables; to learn about these, select Help > Flash Tutorials > Basic Tasks: Create an Application.

 

Set up your workspace

#TOP

First, you'll open the start file for the lesson and set up your workspace to use an optimal layout for taking lessons.

  1. To open your start file, in Flash select File > Open and navigate to the file:

    • In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and Tutorials\Tutorial Assets\ActionScript\Create a Form and double-click simpleForm_start.fla.

    • On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples and Tutorials/Tutorial Assets/ActionScript/Create a Form and double-click simpleForm_start.fla.

    NOTE The Create a Form folder contains completed versions of the tutorial FLA files for your reference.

  2. Select File > Save As and save the document with a new name, in the same folder, to preserve the original start file.

  3. Select Window > Workspace Layout > Default to set up your workspace for taking lessons.

  4. If necessary, drag the lower edge of the timeline (Window > Timeline) down to enlarge the timeline view.

 

Add an input text field to collect form data

TOP

You'll start by inserting a text field in your document in which viewers can enter data. You will also name the text field so you can later reference the text field as you use ActionScript in your Flash application.

  1. Click in the workspace, away from objects on the Stage, so that no objects are selected.

  2. In the Tools panel, select the Text tool.

  3. In the Property inspector, do the following to set text attributes:

    • Select Input Text from the Text Type pop-up menu.

    • Select _sans from the Font pop-up menu.

    • Enter 10 in the Font Size text box.

    • Click the text color box and select a shade of dark blue.

    • Verify that Align Left is selected.

    • Verify that Single Line is selected in the Line Type pop-up menu.

  4. In the Timeline, select Frame 1 of the Input Text layer.

  5. On the Stage, drag the Text tool to create an input text field to the right of the http:// text.

  6. If necessary, use the Selection tool to drag the text field or use the arrows keys to adjust the position.

  7. With the input text field still selected, in the Property inspector, type url_txt in the Instance Name text box.

    You'll refer to the instance name later when you add ActionScript.

 

Add a Submit button to the form

TOP

The Library panel contains a Submit button symbol that you will add to the form.

  1. From the Library panel (Window > Library), drag the Submit button to the Stage and place it over the SubmitURL guide.

  2. Drag the button or use the arrows keys to adjust the position, if necessary.

  3. In the Property inspector, type submit_btn in the Instance Name text box.

 

Move between scenes

TOP

You can move between scenes in the authoring environment by selecting a scene in the Scene panel.

  1. To open the Scene panel, select Window > Other Panels > Scene.

  2. Select Scene 1.

 

Add an error message

TOP

You'll add a message to display if the user clicks the Submit button before entering data.

  1. In the Timeline, with the Branding layer selected, click Insert Layer and name the layer Dialog Boxes.

  2. Select Frame 5 of the Dialog Boxes layer.

    Right-click (Windows) or Control-click (Macintosh) the selected layer and select Insert Blank Keyframe from the context menu.

  3. From the Library panel, drag the Dialog Box-error movie clip to the center of the Stage.

  4. In the Timeline, select Frame 5 of the Buttons layer.

  5. Drag the Try Again Button symbol from the Library panel to the Stage, placing the button below the error message text.

  6. With the button still selected, in the Property inspector, enter tryAgain_btn in the Instance Name text box.

 

Add a confirmation message

TOP

Next, you'll add a message to display when the user submits an entry in the text field.

  1. In the Dialog Boxes layer, select Frame 10.

  2. Right-click (Windows) or Control-click (Macintosh) the selected frame and select Insert Blank Keyframe from the context menu.

  3. From the Library panel, drag the Dialog Box-confirm movie clip to the center of the Stage.

  4. Close the Library panel.

 

Add a stop() action

TOP

When a Flash application loads in Flash Player, it automatically plays in a continuous loop. You use ActionScript to control playhead movement in a timeline. You will add a stop() action in the first frame of your application so the user can make an entry in the input text field.

  1. In the Timeline, with the Input Text layer selected, add a new layer and name it Actions.

  2. Select Frame 1 of the Actions layer.

  3. In the Actions panel (Window > Actions), verify that Frame 1 is selected.

  4. Click in the Script pane of the Actions panel, and type the following comment:

    // Stops the playhead at Frame 1.

    Press Enter (Windows) or Return (Macintosh).

  5. Type stop(); to add the stop action.

 

Add frame labels for navigation

TOP

When the viewer presses the Submit button, you want Flash to jump to either the error message or the confirmation message, depending on what is entered in the text field. Labeling a frame helps you reference it in ActionScript easily. This is helpful for sending the playhead to a specific frame.

  1. Next, you'll add frame labels to help you navigate your Flash application.

  2. Add a keyframe (Insert > Timeline > Keyframe) to Frame 5 of the Actions layer.

  3. In the Property inspector, type error in the Frame Label text box.

    Press Enter (Windows) or Return (Macintosh). The label text and a flag appear in Frame 5 of the Timeline.

  4. Add a keyframe to Frame 10 of the Actions layer.

    In the Property inspector, type confirm in the Frame Label text box. Press Enter or Return.
     

 

Add conditional logic for the Submit button

TOP

With ActionScript, you can have Flash compare information and take action based on criteria you specify. In this example, you'll add ActionScript for Flash to take one action if the user enters no data in the text field, and a different action if the user does enter data.

  1. Select Frame 1 of the Actions layer.

    In the Script pane, place the insertion point after the stop(); code. Press Enter (Windows) or Return (Macintosh).

  2. Type the following comment:

    // Adds conditional logic for the Submit button that

    // validates user input.

    Press Enter or Return.

  3. In the Actions panel, click Insert a Target Path, located at the top of the panel.

  4. In the Insert Target Path dialog box, verify that Relative is selected.

    Click submit_btn on the hierarchical tree, and click OK.

  5. In the Script pane, type a period (.) after submit_btn, and then type onRelease.

  6. With the insertion point after onRelease, type = function (){} in the Script pane.

  7. Place the insertion point between the curly braces and press Enter or Return, and then type if (url_txt.text == null || url_txt.text == ""){ in the Script pane.

    The parallel lines are equivalent to logical or in ActionScript.

  8. With the insertion point still inside the curly braces, press Enter or Return.

  9. Type gotoAndStop("error"); in the Script pane.

    Press Enter or Return.

  10. Place the insertion point after the curly brace and type else{ in the Script pane.

    Press Enter or Return.

  11. Type gotoAndStop("confirm") in the Script pane.

    Press Enter or Return, and type }, and then press Enter or Return again and type };. Your script should appear as follows:

    // Stops the playhead at Frame 1.

    stop();

    // Adds conditional logic for the Submit button that

    // validates user input.

    this.submit_btn.onRelease = function(){

    if (url_txt.text == null || url_txt.text ==""){

    gotoAndStop("error");

    } else {

    gotoAndStop("confirm")

    }

    };

     

Pass data out of a SWF file

TOP

You can send data from a Flash application in various ways--for example, in this lesson you send data to a web server to load a web page in the browser. After the else statement, you'll add the ActionScript to have Flash go to the URL the viewer enters in the input text field.

  1. In the Script pane, place the insertion point in front of the line that reads gotoAndStop("confirm").

  2. In the Actions toolbox, select Global Functions > Browser/Network and double-click getURL.

  3. With the insertion point between the getURL() parentheses, type "http://"+url_txt.text to specify the data that should pass from the SWF file.

    (Do not leave spaces in the code.) Your script should look like the following:

    stop();

    this.submit_btn.onRelease = function(){

    if (url_txt.text == null || url_txt.text ==""){

    gotoAndStop("Error");

    } else {

    getUrl ("http://"+url_txt.text);

    gotoAndStop("Confirm")

    }

    };

     

Write a function for the Try Again button

TOP

A function is a script that you can use repeatedly to perform a specific task. You can pass parameters to a function, and it can return a value. In this example, when the user clicks the Try Again button, a function runs that returns the playhead to Frame 1.

You'll write that function now. In this script, you'll type the frame number, because you did not label Frame 1.

  1. In the Timeline, select Frame 5 of the Actions layer.

  2. In the Script pane, type the following comment:

    // button function returns user to Frame 1.

    Press Enter (Windows) or Return (Macintosh).

  3. Type tryAgain_btn.onRelease = function(){, and then press Enter or Return.

  4. Type gotoAndStop(1);, and then press Enter or Return and type } to complete the script.


 

Test your SWF file

TOP

You'll test your document by entering a URL and checking whether it works as expected.

  1. Select Control > Test Movie.

  2. When the SWF file appears, click Submit before typing anything in the input text field.

    The error message appears.

  3. Click Try Again, and then type the URL of a valid website in the input text field.

    Click Submit.

    Your default browser opens the web page.

 

Summary

TOP

Congratulations on learning how to write a script with conditional logic and send data. In a few minutes, you learned how to complete the following tasks:

  • Add an input text field to a document.

  • Create a button symbol.

  • Add a stop() action.

  • Write a script that validates the form with conditional logic.

  • Pass data out of a SWF file.

  • Write a function.

To learn more about Flash, take another lesson.

 

 
 

Copyright ADOBE - All Rights Reserved Worldwide

 

 

More Topics:

Working with Flash Documents

How to work in Flash WorkSpace

Working with Projects in Flash

Process to Build your First Application in Flash

Using Symbols, Instances and Library Assets in Flash

How to Build Video Player in Flash

How to Work with Color, Strokes and Fills in Flash

How to Create Document in Flash

What is Vector and Bitmap Graphics in Flash

How to Create a Banner in Flash, Part 1

How to Work with Text in Flash

How to Create a Banner in Flash, Part 2

How to Use Imported Artwork in Flash

How to Create a Banner in Flash, Part 3

How to Work with Graphic Objects in Flash

How to Work with Layers in Flash

How to Use Filters and Blends

Working with Graphics in Flash

What is Accessibility Features in Flash

How to Create Motion (Shape Tween & Motion Tween) in Flash

How to Create an Application in Flash

What is Masking in Flash

How to Work with Video in Flash

How to Use Layout Tools in Flash

What are Behaviors in Flash

How to Work with Sound in Flash

How to Create Symbols and Instances in Flash

What is ActionScript in Flash

How to Write ActionScript With Script Assist in Flash

How to Add Button Animation and Navigation in Flash

What is Data Integration in Flash

How to Work with Screens

How to Create a Presentation with Screens

What is Extending Flash

How to Create Multilanguage Text in Flash

How to Create Graphics: Draw in Flash

What is Flash Lite

Ways of Data Integration

How to Create Graphics: Create a Timeline Animation in Flash

Getting Started with Flash Lite in Flash

How to Publish Flash Documents

How to Create Graphics: Making Animations with Easing

Learning Flash Lite 1.X ActionScript in Flash

How to Export Flash Content and Images from Flash

How to Create Graphics: Applying Gradients in Flash

Process of Writing and Editing ActionScript 2.0 in Flash

How to Create Accessible Content in Flash

How to Create Graphics: Apply Graphic Filters and Blends

What is Data and Data Types in Flash

Process of Printing from SWF Files in Flash

Using ActionScript: How to Use Script Assist mode in Flash

Learn Syntax and Language Fundamentals in Flash

How to Create E-learning Content in Flash

Using ActionScript: How to Write Scripts in Flash

Working with Functions and Methods in Flash

Process of Using Templates in Flash

Using ActionScript: Process of Adding Interactivity in Flash

What are Classes in Flash

Control Tag Summary of XML to UI in Flash

Using ActionScript: How to Create a Form with Conditional Logic and Send Data in Flash

What is Inheritance in Flash

What is Data Integration: Overview

Using ActionScript: How to Work with Objects and Classes in Flash

Overview on Interfaces in Flash

What is Data Integration: Using XML for a Timesheet

How to Work with Text and Strings in Flash

How to use Handling Events in Flash

What is Data Integration: Using XUpdate to Update the Timesheet

Learning Animation, Filters and Drawings in Flash

How to Work with Movie Clips in Flash

How to Create Interaction with ActionScript in Flash

How to Work with Images, Sound, and Video in Flash

How to Work with External Data in Flash

What is Security in Flash

How to Debug Applications in Flash

List of Error Messages in Flash

Using Object-Oriented Programming with ActionScript 1.0 in Flash

How to Write Scripts for Earlier Versions of Flash Player in Flash

List of all Keyboard Keys and Key Code Values for using in Flash

Terminology

Introduction to Components in Flash

What are Components in Flash

How to Create an Application with Components

How to Work with Components in Flash

How to Handle Component Events in Flash

How to Customize Components in Flash

How to Create Components in Flash

What is Collection Properties in Flash