Flash
Forms and Database Integration
by
Dave Collier AKA Digitalosophy
This tutorial will guide you through the
steps of sending information into a Microsoft Access
Database using a Flash form and ASP as a server side script.
You will also learn some basic and very useful ASP syntax.
The example I will be using was created in Flash MX 2004.
This will also work for Flash MX, and Flash 5.
Steps:
1. Create the database
2. Create the Flash form and send our variables to our ASP
page
3. Retreive variables form flash and insert the information
into our database
Creating the Database
I'm not going to go into great detail about creating a
database in Microsoft Access, but I will walk you through
it.
-
Create a new database by going to File >
New and save it in your _root directory for now. We will
later move the database outside your _root folder for
security purposes.
-
Create a table in design view and save it
with the name contacts:
-
You will then need to create the field
names and datatypes. Yours should match mine below:
Field Name |
Data Type |
|
ID |
AutoNumber |
*primary key |
FirstName |
text |
|
LastName |
text |
|
Email |
text |
|
Message |
Memo |
|
-
Ok, your done creating your database,
save your file and let's open up flash.
Creating the
Flash Form
-
As I mentioned before this will work using
Flash 5 - Flash MX 2004, so take your pick and open a new
document.
-
Create four input textfields with either
the text tool or ActionScript, and a submit button. Make
sure all textfields are set to input in your properties
panel.
-
Make sure to give your input textfields
variable names. I used:
fname
lname
email
message
You should now have a form
similar to mine:
For now what we are going to
do is use the getURL() syntax so we can test our ASP page.
Later on when we know everything is working we will then
change the getURL() sytnax to loadVariablesNum(). You may be
very surprised to see that we will only be using 1 line of
actionscript to send our data to the ASP page.
Remember you can simply use
the loadVariablesNum() syntax, so if your bold go ahead, but
you won't be able to really test your asp page until your
done.
Creating the ASP
Page
If your familiar with PHP you will
quickly notice that the process to enter information into a
database is the same. The steps we will need to take are as
follows:
-
Retreive the data from Flash
-
Connect to our database
-
Create a SQL query to insert the data we previously
obtained
-
Execute the SQL query.
Receive the Data
from Flash
-
Open notepad or a similar
editing program.
-
Lets start with the basics of ASP. first save
your document as processForm.asp
-
Let's first define what scripting we are
using. ASP scripts are surrounded by the delimiters
<%...%>. Here is the syntax:
<%@language = "VBScript" %>
-
Now lets capture the the variables form our
Flash form we created and give them. Your code show read
as follows:
<%@language = "VBScript" %>
<%
strFirst = Request.Form("fname")
strLast = Request.Form("lname")
strEmail = Request.Form("email")
strMessage = Request.Form("message")
%>
All we are doing here is
setting up variable names and giving them the values we
retrieved from Flash. So when we want to refer to those
values we will be using the new names we just gave them.
{Note this will also work with a HTML
form.}
It's always a good idea to
trouble shoot as you go. So let's make sure were actually
receiving the information before we go ahead and try to do
anything with it. So let's print our variables to the
browser. Add this code to your ASP file.
Response.Write(strFirst) &
"<br>"
Response.Write(strLast) & "<br>"
Response.Write(strEmail) & "<br>"
Response.Write(strMessage)
So now when we test our file the page should display the
information you typed into the Flash form. So far this is
what your code should look like.
<%@language = "VBScript" %>
<%
strFirst =
Request.Form("fname")
strLast = Request.Form("lname")
strEmail = Request.Form("email")
strMessage = Request.Form("message")
Response.Write(strFirst) &
"<br>"
Response.Write(strLast) & "<br>"
Response.Write(strEmail) & "<br>"
Response.Write(strMessage)
%>
Connecting to our Database
Great now that we have our
data captured let's go ahead and connect to the database.
Here is the syntax that allows us to connect to the database
we created:
MyPath=Server.MapPath("example.mdb")
Set conn = Server.CreateObject("ADODB.Connection") '
conn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & MyPath
Let's test our page and make
sure we don't get any errors. Your final code should look
like this:
<%@language = "VBScript" %>
<%
strFirst =
Request.Form("fname")
strLast = Request.Form("lname")
strEmail = Request.Form("email")
strMessage = Request.Form("message")
Response.Write(strFirst) &
"<br>"
Response.Write(strLast) & "<br>"
Response.Write(strEmail) & "<br>"
Response.Write(strMessage)
MyPath=Server.MapPath("example.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & MyPath
%>
Creating the SQL
Query
If your not familiar with
SQL the syntax for INSERTING is:
INSERT INTO table_name
VALUES (value1, value2,....)
Our full SQL query will read
as follows:
SQL = "INSERT INTO
contacts(FirstName, LastName, Email, Message) VALUES ('"&strFirst&"','"&strLast&"','"&strEmail&"','"&strMessage&"')"
Again let's avoid future
troubleshooting and print our SQL statement your code should
read as follows:
<%@language = "VBScript" %>
<%
strFirst =
Request.Form("fname")
strLast = Request.Form("lname")
strEmail = Request.Form("email")
strMessage = Request.Form("message")
Response.Write(strFirst) &
"<br>"
Response.Write(strLast) & "<br>"
Response.Write(strEmail) & "<br>"
Response.Write(strMessage)
MyPath=Server.MapPath("example.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & MyPath
SQL = "INSERT INTO contacts (FirstName, LastName, Email,
Message) VALUES ('"&strFirst&"','"&strLast&"','"&strEmail&"','"&strMessage&"')"
Response.Write(SQL)
%>
Executing the SQL
Query
Now that everything is
working let's go ahead and execute our SQL query. The syntax
is as follows:
conn.Execute(SQL)
At this point we really
don't need our asp page to print out our variables and SQL
statement. So let's clean up our ASP unless you don't want
to or just comment out the Response.Write lines. (the '
character is used to comment in ASP)
Final Code:
<%@language = "VBScript" %>
<%
strFirst =
Request.Form("fname")
strLast = Request.Form("lname")
strEmail = Request.Form("email")
strMessage = Request.Form("message")
MyPath=Server.MapPath("example.mdb")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & MyPath
SQL = "INSERT INTO contacts (FirstName, LastName, Email,
Message) VALUES ('"&strFirst&"','"&strLast&"','"&strEmail&"','"&strMessage&"')"
conn.Execute(SQL)
%>
Ok that's it. If you've
gotten this far you've done a nice job. I tried to keep the
ASP as simple as possible. None of the code is written in
stone, it's just the way I like to do things. If your
getting error's or if your information isn't getting stored
in the database retrace your steps, and remember to always
print before you execute. One final thought
is about security. If you keep your database inside the
_root folder of your site, your database can now be accessed
by anyone. You should always keep your databases outside the
_root of your web folder. The only thing you will need to
change is the path to the database in your ASP script.
Change:
MyPath=Server.MapPath("example.mdb")
To something like:
MyPath=Server.MapPath("../database/example.mdb")
If you have any questions, feel free
to post on the forums by
clicking here.
|
Digitalosophy |
|