If you recall, I have mentioned earlier that the LoadVars object has only one event, and one method for that event. The method is an empty function, OnLoad, which must be written by you. Well, here's that function. But before I explain it, let me skip ahead a bit and then we'll come back to it.
There is another method that belongs to LoadVars that we are using in this movie, and it doesn't belong to the LoadVars event, but essentially creates the event itself. This is the sendAndLoad method, which we'll encounter in a moment. But basically this method takes the variables from the LoadVars object (formData), URL encodes them, sends them to the server, then loads the data sent back from the server, URL decodes that, and populates our second instance of LoadVars (replyData). Once all this is done, it calls the onLoad function, passing a parameter of "success", which is a Boolean true or false indicating whether all this was accomplished successfully or not. It then sets the LoadVars property loaded to the correct variable so that we can later test for "success" if need be.
Whew! That's a lot more than LoadVariables ever did in previous versions of Flash! We'll come back to loadAndSend in our next function, but we have to define this one first, since it's called by loadAndSend.
We want to ensure that the message box is enabled, so we start by setting that parameter to true. Then if the event was successful, we set a new icon for the box, a new title for the box, and set a new message to appear to the user. After that we make the box appear by setting it's _visible parameter to true. The message box will remain visible on the screen until the user clicks the "OK" button, which resets errBox._visible back to false and then executes our last line of code which calls the clrData function, clearing the screen and variables.
If the event is not successful, we do the same steps with a different icon and text. However, this time, we don't clear the data, so that the user can attempt to re-send it to the server after the message box disappears.
MX doesn't tell us what the time-out is for this event, but I discovered (again, with a bit of trial-and-error) that it's not very long, and since it all happens within a single method, we can't very well insert a delay timer or such device to make it wait on the server. In fact, I discovered if I sent the email before I returned the data, that loadAndSend would time out before downloading the data. You'll see in the PERL script (incuded with this tutorial) that I send the data back to flash from the server as soon as I get it. So it appears possible that server lag or network congestion could cause a timeout in this method, which is why I designed it so that the user can attempt to re-send on an error.
The only data I send back in this script is the user's name, which of course we already have. But a CGI script must return something and this also acts as a flag to loadAndSend that the operation was a success. Of course depending on the needs of your application, you could just as easily be returning prices, store locations, or any conceivable type of data from the server.
So now let's move to our last function, goPostal, which ties it all together!
|» Level Intermediate|
Rating: 9 Votes: 81
|Tom Watson is a Flash developer currently residing in Nashville, Tennessee. He's been an independant contractor in the computer business since 1989, working on web projects since the web was introduced to the Internet in 1994.|
|Download the files used in this tutorial.|
|Download (569 kb)|
|More help? Search our boards for quick answers!|