Flash Php Mysql

Embed Size (px)

Citation preview

  • 8/18/2019 Flash Php Mysql

    1/9

    Tutorial: Using Flash, php and MySql, keep persistentinformation; Flash radio and push buttoncomponents

    A typical Flash application, like a simple HTML web page, does not collect and storedata on the server computer. To store information, you need to use a file or a databaseand connect to the file or database using so called server side or middleware software.!imilarly, a typical php application accepts form data from a HTML form and producesan HTML document. This e"ample demonstrates using Flash and php to store data in ande"tract data from a My!#l database. $oth php and My!#l are considered %pen !ourcetools.

    The application keeps track of votes cast for & choices. 't is intended to represent a polling application, not a voting application. (lease note that a real voting application

    would need to incorporate features to allow only legitimate voters to vote and to onlyvote once. A debate is going on now concerning various systems for electronic voting.%ne feature considered critical by many, though not all, is that the system produce a

    paper trail for each vote. This toy application does not include any of these features. Theintent is to demonstrate how Flash can connect to a php script.

    $efore continuing, one more warning) A basic contract with an 'nternet !ervice (rovidergenerally would not include support of php scripts or My!#l *or any other+ database.This application and tutorial assumes php and My!#l support and, moreover, assumesthat a table has been created and initiali ed to hold the polling data.

    't is assumed that the reader has some background using Flash. This document wille"plain the features of the application, not give step by step instructions on how toreplicate it.

    The initial screen for the Flash interface is the following)

  • 8/18/2019 Flash Php Mysql

    2/9

    'f the person responds by clicking on the radio button ne"t to %ther and then !ubmit vote,the following screen results *at this point for this application. ' do not know where these-votes- came from perhaps my students+.

    The Flash interface consists of• the static te"t field with the words /'f you had to vote today, would you vote for

    0./• & radio button components• 1 push button component• A dynamic te"t field, marked as accepting HTML tags and with a boundary

    There are two layers) one layer, named board, holds all the graphics2 the other layer,called actions, holds the Action!cript. This is not necessary but is a standard way toorgani e an application.

    The static te"t field is created in the usual way, using the te"t tool *icon A+.

    The radio buttons are created by clicking on the radio button icon on the component panel and dragging a button to the !tage. The screen shot below shows the (roperty panel as set up for the $ush button. 'n this case, the label and the data are similar. Thatdoes not have to be the case. 3otice also the 4roup 3ame. All four buttons would havethe same group name. This works to make the Flash engine make sure that only one

    button is clicked. 3otice that there is no 5hange Handler. This is because for thisapplication, we wait for the -voter- to click on the !ubmit 6ote push button. The 'nitial

    state and the Label (lacement were left unchanged. 7ou need to do this for as manytimes as you want choices.

  • 8/18/2019 Flash Php Mysql

    3/9

    The ne"t figure shows the screen when the push button is selected. As with the radio button, you click on the push button icon in the components panel and drag an instance tothe !tage. The key properties to change are the Label and the 5lick Handler. Thegotoserver is a function defined in the actions layer, frame 1 *only frame+ of the Timeline.

  • 8/18/2019 Flash Php Mysql

    4/9

    The dynamic te"t field is created using the te"t tool *the A+, choosing 8ynamic *asopposed to !tatic or 'nput+. 3otice that the name display is entered into the instancename position and not the 6ar position. 3otice also that the options are selected forMulti line, HTML *the 9: icon+ and border. The HTML tag support is limited to tagssuch as 9b: for bold, 9br: for line break, and *not used here+ 9a: for hyperlink. There isno support for table tags and if you use 9p:, you need to use 9;p: to get noticeableresults.

  • 8/18/2019 Flash Php Mysql

    5/9

    The ne"t logical step is to show and e"plain the gotoserver function. 't and anotherfunction are coded into the actions layer, frame 1, using the Actions panel. This is framecode, not associated with any ob>ect. 3ote) the push button component definition hasmade the connection between the gotoserver function and the event of clicking the

    button.

    The screen shot shows all the Action script)

  • 8/18/2019 Flash Php Mysql

    6/9

    The variable connect is defined using the Load6ars*+ function. That is, it is an ob>ect oftype Load6ars, created to carry information to and from the php script. The functiongotoserver does three things.

    connect.onLoad ? getback2 specifies that when the Load operation is complete,call the function getback. 3otice that this is all this statement does. 't does not initiate thecall to the php script.

    connect.data ? president.getvalue*+2 defines a new property of the connect ob>ect. Thenew property is named data. 'ts value is e"tracted from the president radio button groupand is the value of that group) /$ush/, /@erry/, etc.

    connect.sendAndLoad*/addtoresults .php/, connect, /(%!T/+2 makes the call to the

    file on the server named addtoresults .php. The connect ob>ect is used to bring backinformation *all the e"amples ' have seen use the same Load6ars ob>ect, but, presumably,this could be different+. Lastly, the method of transmission is (%!T. (eople familiar withHTML forms will remember the choice of 4BT versus (%!T.

    The call to the php script may take some time.

  • 8/18/2019 Flash Php Mysql

    7/9

    script and assigns it to the display dynamic te"t field instance using the htmlTe"t property. This means that whatever is assigned is interpreted as HTML te"t.

    The php file must take the data, access and update the appropriate record in the My!#ldatabase, then access all the records *all &+ in the database, format the results, and return

    this string of characters to the Flash program. !tructured Cuery Language, !CL, is usedto access the database. The table in the database is named votes. Bach record has twofields *actually, each record has an id field, but that is not used.+) candidate and votescast.'n php, variable names start with dollar signs. The setting of the data sent back to theother program is done using the name you decide on *here, display+ and print statements,starting with print /Ddisplay ? /2

    Here is the code *with my user name, password and database name replaced by #uestionmarks. 7ou will need to change this to values for your account+)

  • 8/18/2019 Flash Php Mysql

    8/9

    $lin+0% does the #uery. 3o error checkingis doneE

    $-'er, = &S3839T : ;!O 4otes&% 8efine another

    #uery, this onee"tracting all therecords from thevotes table.

    $res'lt = m,s-l_db_-'er, $)*name $-'er,$lin+0%

    This statementdoes the #uery,return the resultsin the resultvariable. This iscalled a recordset.

    (hile $ro(=m,s-l_ etch_arra, $res'lt00 > The whilestatement uses a built in functionin php thate"tracts anassociative arraycorresponding toa row in the

    result recordset.'t will return falsewhen there are no

    more rows*meaning nomore records+.

    print &

  • 8/18/2019 Flash Php Mysql

    9/9

    A 5lose the while? 5lose php

    This is the applicationE ' wrote two more php scripts *in addition to scripts defining thetable and initiali ing it with & records+, none connected to Flash. %ne reset the values to

    ero and the other showed the values.

    More on php can be found in my book) Creating Database Web Applications with PHPand ASP , 5harles =iver Media, publishers.