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:
Those were the two new features of the LoadVars object we are using. Now let's get back to the example:
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.
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.
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.