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:
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 = 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:
Dim myVar
myVar = Trim(Request("whatWasSent"))
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:
<%@Language=VBScript%>
<%Option Explicit%>
<%
Dim inNum, answer
inNum = Trim(Request("sentNum"))
'''CREATE THE ANSWER
answer = inNum*inNum
Response.Write("answer=" & answer)
%>
You should see something like this in your browser:
http://MYSERVER/squareNum.asp?sentNum=10
And you can change the number in the URL to see more results.TIPYou 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:
answer=100
That will do it for the ASP part of this example; next up is the Flash part.
http://MYSERVER/example.asp?var1=sample&var2=anotherSample
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:
In the preceding code, two variables are ready to be sent to an awaiting ASP page.TIPAs 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:
var test_lv:LoadVars = new LoadVars();
test_lv.sentName = "David"; //Sending a string
test_lv.sentAge = 25; //Sending an integer
The preceding line of code has the following parameters:
test_lv.sendAndLoad(URL, returnToObj, method);
- 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:
1. | Create a new Flash document and save it as squareNum.fla . |
2. | Create a new layer in the timeline (so you have two in total) and call the top layer actions and the bottom layer content . |
3. | In the first frame of the Actions layer, place this code in the Actions panel:
|
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.
- 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
- Type
Dynamic - Show Border Around Text
True - Font Size
14 - Instance Name
answer_txt
- Type
- 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.
- 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:
Now the answer will appear in the bottom text field when it is returned.
answer_txt.text = this.answer; - 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");
}