Join Anastasia McCune for an in-depth discussion in this video Preparing the input data, part of Flash: Best Practices for Banner Ads.
Let's write our ActionScript that will grab the name and email address the user provides and send it to another email address to complete the user's request for a cooking guide. So let open our Actions panel on Frame 1. The first thing we're going to do is create a container that will hold the user's name and email address. So I'm going to type this at the very top of the frame. I'm also going to create some code comments so we know what each line of code does. So first we're going to create a container for the variables.
So we're going to create a variable and we're going to call it mailVars and we're going to type it as a URLVariables. Now we're just going to set it to new URLVariables, we're actually creating a new instance of the class. We can see here again that Flash has tried to help out with this handy auto complete, but again we don't need this line of code so we can delete it. Now we're going to create a second variable that will contain where we want the information to be sent to. So I'm going to type a code comment.
So I'm going to create a new variable called mailAddress and data type it as a URLRequest. Now we're going to tell Flash that the place we want to send all this data is to http://www.twotreesoliveoil.com/email.php. Now you don't have to do anything about this PHP file. There's a live version of it sitting up on the web server at twotreesoliveoil.com.
Now in case you're wondering, tasteofcalifornia.com and twotreesoliveoil.com are both fictional companies. However twotreesoliveoil.com is the parent company of Taste of California, so that's where our PHP file is living. You may have also noticed that when we typed in this line of code Flash was trying to be helpful again and auto completed this line of code up here. That's another line of code we don't need so I'm going to go ahead and delete it. Now regarding this PHP file, you don't have to do anything about it a live version of it is sitting up on the web server at twotreesoliveoil.com.
Let's go ahead and take a look at it in Dreamweaver. So you can open up email.php out of the Chapter 6_3 start folder. So taking a quick glance of this file, we can see that it's looking for who to send the email to, what the subject of the email should be, what the name of the person who filled out the form is, the email address of the person who filled out the form so they can be sent a cooking guide and then a message. So to be totally clear about this here, this script is going to generate an email that going to be sent to somebody at tasteofcalifornia.com, telling them the name and email of someone who's requesting the cooking guide.
Then they'll send the cooking guide to the person's email. Now you might be thinking what if hundreds or thousands of people request this cooking guide? The person at tasteofcalifornia.com who is responding to those requests is going to be overwhelmed. And that's totally true. Instead of having our Flash piece generate an email, it might make more sense for it to submit the user's information to maybe a database and then create some kind of automated process to send out the cooking guides. But submitting to databases can get a little involved from the database end and it's not the subject of this course.
Generating emails from Flash is a commonly built functionality, so to have a working example for a course today we're going to stick with the email model. So we have seen what the PHP file is expecting. Let's go back to our Actions panel in Flash and write the supporting code. So mailVars is the ActionScript container that's going to contain all the info our PHP file is expecting. We know that the PHP file is looking for a piece of data called sUserName. So here in our onSummit function let's get rid of our trace and write the code.
So I'm going to type maleVars. sUserName is equal to nametext.text. This is saying that sUserName should be equal to the text that the user types in in the name text input box that we just created. Now let's write a second line of code this line is very similar. We're saying that sUserEmail should be equal to the e- mail address that the person types into the email box that sitting on the stage.
The PHP file is also looking for something called sCookGuideEmail. This the email address of the person at tasteofcalifornia.com who is going to receive our email request for a cooking guide and respond to it. For our testing purposes during development let's make this equal to firstname.lastname@example.org. When you actually deploy this project in real life you want to replace that email with the address of a real person. So in this line of code we're going to create the message that actually appears in the body of the email. It's going to say please send, the name of the person, a cooking guide, at the persons email address.
The next is going to construct the subject line of the email. It's simply going to say a new cookbook request for the persons name.
Now let's actually send the email. Here's the code to do so and I'm going to type some comments, so we know what each line of code does. So our first line of code is going to attach the variable to the email address object. Our second line of code is going to be telling Flash though we want to send the variables using the post method as opposed to the get method. If you want to know more about the differences between get and post you can look up the URLRequest method in the Flash help.
Finally, our last line of code actually send the email. Let's go ahead and test our movie, let's fill out the form with a name and an email address, and hit the Submit button. Now let's go over to the tasteofcalifornia1 email inbox and see if our email actually showed up. Okay, so the email has arrived and let's go ahead and open it up.
Here we can see that we've made a small error. It says please send undefined a cooking guide and then the email address. Somewhere we have a typo in our code, so let's go back and look, and this is an example of really the best way to test your code, by actually running your example and seeing if it works. When it doesn't, doing some bug squashing. So our error was in this part of the code it said please send undefined and the reason it did that is because our users name is held in a variable called at sUserName. Here we've only typed sName.
So I'm going to correct this by typing in the word User. You can also see in our email that we could use an extra space right here between word undefined and A and the place that we add that in our code is right here. Let's test one more time and let's see what the new email looks like.
Yup this one looks much better. Our form is working and our email is being sent with all the variables being sent through properly. So we wrote a lot of code here and this was a really good example of how sometimes the code doesn't work the first time. Testing your movie multiple times so you can be sure to do some good error checking before you send the project out here client is the best way to debug your project.
- Understanding terminology
- Gathering requirements
- Understanding whether to use Flash or HTML5
- Setting up an ad and using guide layers
- Adding clickability
- Optimizing images and text
- Publishing final files and images
- Creating input text fields
- Creating a print job
- Handling print errors
- Tweening between collapsed and expanded states
- Adding timeline control
- Addressing security issues