Sending and Receiving Data

The most dynamic content you can have comes from interaction with the user. When users interact with your project, either through search engines, forum boards, or even comments found in most blogs, they are not just receiving contentthey are requesting content, and in some cases, even adding content. Some of the most popular sites rely on "content-on-demand" principles to keep users coming back. They do not just give information; they ask the user what they want to see and provide it.

So far we have only loaded content from our ASP pages, but in this section, you'll learn how to make it truly dynamic by sending data to the ASP page and then receiving data back.

The first step in this process is setting up the ASP page to receive data from the user.

Receiving Data in ASP

To receive data in ASP, we will use the Request object like this:

 Dim myVar myVar = Request("whatWasSent") 

The preceding code sets the variable myVar equal to the value of whatWasSent.

We will also be using the trim function to get rid of any unnecessary spaces. This function works like this:

 Dim myVar myVar = Trim(Request("whatWasSent")) 

The preceding code produces the exact same result, except that all extra spaces are removed. This is very important when working with data coming from the client side, especially when searching for items in a database. An extra space can be the difference between getting all the search results and getting none.

That was the basics; now let's look at a working example. This next example will create a small ASP page that will receive a number, square it, and send it back to the user.

This is the code for the ASP page:

 <%@Language=VBScript%> <%Option Explicit%> <%   Dim inNum, answer   inNum = Trim(Request("sentNum"))   '''CREATE THE ANSWER   answer = inNum*inNum   Response.Write("answer=" & answer) %> 

This script will take a number sent to it through the Request object, multiply it by itself, and then send it back out as a name/value pair. Save this file as squareNum.asp and post it to your server.

If you want to test this page right now, you can, by passing the sentNum tHRough the URL:


You should see something like this in your browser:


And you can change the number in the URL to see more results.


You can always test your ASP pages by passing information through the URL in the browser. The first variable after the path to the page must follow a question mark (?) like the preceding example, but the rest of the name/value pairs must be separated by ampersands (&) like this:


That will do it for the ASP part of this example; next up is the Flash part.

Sending and Receiving in Flash

When sending and then receiving information in Flash, the LoadVars object will still be used. However, there are a couple new things we need to go over. The first is how to set the data being sent out.

Setting data for an instance of the LoadVars object to send out is like setting a property. Here is an example setting two variables to be sent out:

 var test_lv:LoadVars = new LoadVars(); test_lv.sentName = "David";  //Sending a string test_lv.sentAge = 25;     //Sending an integer 

In the preceding code, two variables are ready to be sent to an awaiting ASP page.


As a personal practice, I usually name variables that are being sent out to middleware pages starting with "sent." This way, I don't get them confused with the data being returned that may have a similar name.

That was setting up the data to be sent out; now we need to actually send it using the sendAndLoad method of the LoadVars object. It looks something like this:

 test_lv.sendAndLoad(URL, returnToObj, method); 

The preceding line of code has the following parameters:

  • URL The path where your server-side script is. It can be absolute or relative.

  • returnToObj The LoadVars object where the return data is being returned to. Most often, this will be the same object calling the sendAndLoad method.

  • method Either the GET or POST method for sending information through HTTP.

Those were the two new features of the LoadVars object we are using. Now let's get back to the example:


Create a new Flash document and save it as squareNum.fla.


Create a new layer in the timeline (so you have two in total) and call the top layer actions and the bottom layer content.


In the first frame of the Actions layer, place this code in the Actions panel:

 //The object for communicating with the ASP page var sNum_lv:LoadVars = new LoadVars(); //When the object receives data back from the ASP page sNum_lv.onLoad = function(success){   if(success){    trace(this.answer);   }else{    trace("There was an error connecting");   } } //set the number we are sending sNum_lv.sentNum = 15; //send and receive the data sNum_lv.sendAndLoad("http://MYSERVER/squareNum.asp", sNum_lv, "POST"); 

Most of the preceding code is not new, as we have used it throughout this chapter. First, the LoadVars object is instantiated. Then the callback event onLoad is created for when data is received. After that is what may appear new, we set the variable we are sending to our ASP script to 15 and finally call the sendAndLoad method. Test your movie at this point by choosing Control, Test Movie.

Close down the test screen after you see how the data is being sent from Flash, and then the answer is received back.

Next up is the interface so we can change the data being sent (and consequently, the answer being returned) as often as we like without having to alter the code.

  1. Back on the timeline, in the Content layer, draw two text fields, one on top of the other (with some spacing so we can place a button between them), both about 40x20 in size. Here are the properties for the top field:

    • Type Input

    • Show Border Around Text True

    • Font Size 14

    • Instance Name num_txt

    The rest you can leave at default settings. Next are the properties of the bottom field:

    • Type Dynamic

    • Show Border Around Text True

    • Font Size 14

    • Instance Name answer_txt

    You can use static fields to label each of the fields as "Number" and "Answer" if you like.

  2. Now you will need a button to trigger the call to the ASP page. You can make your own or use one from Window, Common Libraries, Buttons. Just place it between the two fields and give it an instance name of square_btn. Your screen should now look like Figure 21.4.

    Figure 21.4. The stage is now set to work with the ASP page.

Now that we have the fields and the button on the stage, we need to change the ActionScript a little bit to be able to grab the info from the first field and place the answer in the second field.

  1. The first thing to change is what happens to the answer when it returns. Right now it is being sent to the Output panel through the trace function. Remove that line and put this line in its place:

     answer_txt.text = this.answer; 

    Now the answer will appear in the bottom text field when it is returned.

  2. Now we want to send the information to the ASP page when we release the button we put on the stage, so remove the last two lines of code (plus their comments) and put this in its place:

     square_btn.onRelease = function(){   //set the number we are sending   sNum_lv.sentNum = num_txt.text;   //send and receive the data   sNum_lv.sendAndLoad("http://MYSERVER/squareNum.asp", sNum_lv, "POST"); } 

The only difference between this code and the code we just cut (besides this code now residing in the onRelease event of the button) is that instead of hard-coding the number we are sending, we grab it from the top text field. Now you can test the movie again, place a number in the top field, click the button, and see the result. And now you do not have to change the ActionScript to see a different result; just change the top field and click the button again.

Could this have been done in Flash? Yes, but it is a good example of sending information to the server and letting the server do the work and send you the answer back. As you work more and more with the server, you will see that when it comes to mathematical functions, servers are much faster than Flash is, especially with very complicated algorithms. It's another reason why server-side scripting and Flash work so well together.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: