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

Error. Page cannot be displayed. Please contact your service provider for more details. (19)



Related Posts


Subscribe to this Post

21 Responses to “How To Send Data From Flash To ASP/PHP Without A Page Refresh”

  1. Internet Marketing Legacy Says:

    Patrick, Thanks for this article. I’m working on a simple flash program for personal use and this comes in quite handy. Thanks again!

  2. Patrick Burt Says:

    My pleasure. If you need any help with anything else, let me know. :)

  3. vimalraj Says:

    hi
    I need to create flash character builder in flash can u help me plz

  4. Patrick Burt Says:

    Hi Vimalraj, I suspect you’re looking more for the basics… Somethings this article doesn’t cover. Consider lynda.com or kirupa.com for learning the basic strings of Flash and Actionscript.

  5. Anil Says:

    Flash and PHP – Send and Receive data

    http://flashactionscriptandanimation.blogspot.com/2008/06/flash-and-php-send-and-receive-data.html

  6. Patrick Burt Says:

    Thanks for the link Anil.

  7. jerome Says:

    Hi,

    I am able to send my data by mail using php which collect data from my flash form.
    The next step i want to achieve will be sending these data from php or flash (whatever i need to use) to populate an html page. Could you please help me??

    Thanks for any help,

    Regards,

    Jerome

  8. Kit Says:

    Hi patrick,

    I was reviewing your post and was wondering if you may have encounbered an issue i am having. I am submitting some data to an ASP page using loadVars in a Flash 8 AS2 file. The data is a series of coordinates and so the amount of data gets pretty big. I am not aware, however of a size limitation for data which is POSTed to a script. And yet this does fail when the data length gets too large.

    Any thoughts?

    Regards,

    Kit

  9. Patrick Burt Says:

    There shouldn’t be a limit to what’s being posted.. hm. is somethign happening with apostrophes? when the data length gets too large, do you have some dodgy characters that might be getting included?

  10. Kit Says:

    Ya i thought that too. it was really strange. I have been trying to debug using Firebug in Firefox and monitoring the Net…but none of the Flash data that was posting was being shown. It appears (after creatiing a HTML form and submitting data to the same ASP processing script, that the field that contains the coordinates is being truncated and causing a ODBC Drivers error ’80040e21′
    [Microsoft][ODBC SQL Server Driver]String data, right truncation . So i guess i am debugging ASP now.

    THanks for the update, i will keep you posted.

  11. Patrick Burt Says:

    In SQL, make sure your field type is set to something like “varchar(MAX)” or “text” so there isn’t a hard cap on how much you can throw into the DB.

  12. Sushil Says:

    Hi Patrick,

    I’m running a flash movie under a frameset which popups a window using loadvars’ send method with data being send by POST. However in IE6, the poped up window is not able to receive any POST data. The calling java servlet too getting null parameters.It is not either maintaining the session too. If data sent through GET, it works perfectly

    This happens only in IE as in Firefox it works fine. Any clue on this problem?

  13. FearAndBullets Says:

    I need something like this.

    I’ve done this in the past with .asp server side includes and i can’t remember how i did it.

    I have a flash header w/nav that resides in a .asp page. It needs to load some elements of a page so i don’t have to do a complete page refresh. I don’t need a database to communicate back to the flash or to the content… just and index.asp w/flash nav changing the include on the page.

    PLEASE HELP!

  14. troy Says:

    seems I get a pop-up of the intoDatabase.asp. The form submission works, but it is a bummer that there is also a pop-up. I am using IE7, have not tested on other browsers….

  15. Rune Says:

    would it be possible for anyone to sende a zipped folder with the requred files despribet in the artical above (of cause with the db login blanked out). I belief that i’m fairly close on getting this to work, but I’m not sure if I have understand everything correct. A sampel of a set up that works would be a great help!

  16. Jax Says:

    HI PAT.

    I have creatied a small game in flash AS2. I would like to maintain the high score of multiple users that play it as it is available in other online flash games. Can u help.. please…

    Thanx a lot.

  17. ruppon Says:

    Hi Pat!

    Excellent very very helpfull solution!
    I met some cross-domain issues and your article resolve them all!

    Thanks, cheers!

  18. JenlFago Says:

    NiksNita say: Quite right! I think, what is it excellent idea.

    _____________
    ciallis
    Missouri
    0

  19. Girish Says:

    Hi,
    I am facing data limit.
    is there any limit to send xmlData to ASP page using AS 2.0. send Method ?

    Thanks
    Girish

  20. WEWEF Says:

    working perfectly … tested today… exactly what im looking for … after several boring hours of googleing….

    thanks a lot…

  21. dewa Says:

    Hi All,

    my name is dewa,

    i have the problem to passing data from flash to asp classic, may somebody help me ?? please :(
    here my action script 2 :
    on (press) {
    var sendv:LoadVars = new LoadVars;
    var receive:LoadVars = new LoadVars;
    var resultL:String;

    sendv.toASPnip = nip_txt.text;
    _root.nip = nip_txt.text;
    _root.idgame = 1;
    sendv.toASPidgame = _root.idgame;

    sendv.sendAndLoad(“http://localhost/janganemberdong/asp/login.asp”,receive,”POST”);
    receive.onLoad = function(){
    resultL = “test”;//this.toFlash;
    if (resultL == “suc”) {
    _root.gotoAndPlay(“3″);
    }else{
    if (resultL == “errGamePlay”){
    _root.gotoAndPlay(“11″);
    }else {
    stop();
    }
    }
    }
    }

    and he my asp classic :

    0 then
    response.write(“&toFlash=suc”)
    end if

    rs.close
    set rs = nothing

    %>

    i hope someone can help me … please :( i need some help guys,

    thanks,