How To Send Data From Flash To ASP/PHP Without A Page Refresh

Wednesday, November 28th, 2007

Categories: Adobe Flash

RSS Comment Feed

Trackback

AddThis Social Bookmark Button

This article will deal the how to of sending form data from Adobe Flash to your PHP/ASP file so it can be inserted into a database (or manipulated for whatever other reason). The trick is making sure a new window doesn’t popup and the page doesn’t refresh to run through the ASP/PHP code. That’s the one issue I’ve noticed with some downloadable example of contact or email form examples. A new window pops up and often says, Thanks for the email! I’ll give a really bare-bones example where no new windows popup and the window containing your Flash isn’t refresh. This articles example will show an example done in ASP, but this will work with any other programming language.

NOTE: This is not for beginners.

Components

  • Flash File
  • HTML with Flash File
  • ASP/PHP/etc Page That Will Process Form Data
  • HTML page with Frames.

Concept

What we’re going to do is set up a Flash File that will simply send form data to an ASP file. Our HTML page with frames will have two frames. One frame will contain the HTML with our Flash, the second will contain a sandbox that will allow the ASP file to refresh without ever affecting the main page we’re browsing. We’re going to hide the second frame so that we don’t ever see the refreshes happening.

I know what you’re thinking, frames are lame. In our example though, you won’t even notice it being there.

Flash File

Create a new Flash file, select the first blank frame you’re given, bring up the ActionScript window and copy and paste the following code:

stop();
varsToSend = new LoadVars();
varsToSend.firstVar=”Pat-Burt”;
varsToSend.send(“intoDatabase.asp”,”process”,”POST”);

Explanation
stop – makes sure that your Flash movie doesn’t loop and constantly send “Pat-Burt” to your ASP file.
varsToSend – our LoadVars() object that contains all the variables we’ll be sending to our ASP file.
firstVar – a variable, you can have any number of these.
send – sends the vars in varsToSend to intoDatabase.asp
process – the frame name (we’ll use this later)
post – how we’re sending the data to the database

HTML Frame Page

Create a new HTML page, strip the body tags and throw this in:

<FRAMESET rows=”100%, 1″ >
<FRAME src=”flash.html” name=”flash” noresize frameborder=”0″>
<FRAME src=”
intoDatabase.asp” name=”process” noresize scrolling=”no” frameborder=”0″>
</FRAMESET>

Explanation
Frameset – creates the frames, defines the first to have 100% height, and the second to have a height of 1 pixel.
flash.html – Our HTML file with our Flash file inserted. It is named “flash”.
process.html – Our HTML file where our ASP file will be reloaded to insert data into the database. It’s located in the frame sized as 1 pixel.

ASP File

This is the simplest version I could present. Keep in mind you will need to define db which should reference your connection string to your database.

db.execute(“INSERT INTO database (message) VALUES (‘”&request.form(“firstVar”)&”‘)” )

Explanation
request.form(“firstVar”) – the name of the variable in our Flash file
database – our database name
message – the column name in our database

HTML with Flash File

Insert your Flash file in whatever form you prefer. I recommend SWFObject.

Put Them All Together And Make Sure You…

  • Name the HTML file with your Flash: flash.html
  • Name your ASP file (in this example) to intoDatabase.asp
  • Provide a <noframes /> alternative where the page is loaded in a new window in the off chance someone has frames turned off
  • Don’t get to complicated. Get everything working in its simplest form before you add extra features

Good luck, hope that helps. :)

Bookmark this blog using any bookmark manager!
Subscribe to Pat-Burt.com via RSS
Tnx.net