Tutorial: Using Flash, PHP and Mysql, Keep Persistent Information Flash Radio and Push Button Components
Tutorial: Using Flash, PHP and Mysql, Keep Persistent Information Flash Radio and Push Button Components
If the person responds by clicking on the radio button next to Other and then Submit vote,
the following screen results (at this point for this application. I do not know where these
'votes' came fromperhaps my students).
The next 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 to
the Stage. The key properties to change are the Label and the Click Handler. The
gotoserver is a function defined in the actions layer, frame 1 (only frame) of the Time
line.
The dynamic text field is created using the text tool (the A), choosing Dynamic (as
opposed to Static or Input). Notice that the name display is entered into the instance
name position and not the Var position. Notice also that the options are selected for
Multi-line, HTML (the <> icon) and border. The HTML tag support is limited to tags
such as <b> for bold, <br> for line break, and (not used here) <a> for hyperlink. There is
no support for table tags and if you use <p>, you need to use </p> to get noticeable
results. When the text field was created, the words Results so far. were entered into the
field. This will be over-written by the results of the php script.
The next logical step is to show and explain the gotoserver function. It and another
function are coded into the actions layer, frame 1, using the Actions panel. This is frame
code, not associated with any object. Note: the push button component definition has
made the connection between the gotoserver function and the event of clicking the
button.
The screen shot shows all the Action script:
The variable connect is defined using the LoadVars() function. That is, it is an object of
type LoadVars, created to carry information to and from the php script. The function
gotoserver does three things.
connect.onLoad
= getback; specifies that when the Load operation is complete,
call the function getback. Notice that this is all this statement does. It does not initiate the
call to the php script.
connect.data = president.getvalue(); defines a new property of the connect object. The
new property is named data. Its value is extracted from the president radio button group
and is the value of that group: "Bush", "Kerry", etc.
connect.sendAndLoad("addtoresults2.php", connect, "POST"); makes the call to the
file on the server named addtoresults2.php. The connect object is used to bring back
information (all the examples I have seen use the same LoadVars object, but, presumably,
this could be different). Lastly, the method of transmission is POST. People familiar with
HTML forms will remember the choice of GET versus POST.
The call to the php script may take some time. When it is done, the getback function will
be invoked. This code contains no error checking for the file not existing or some other
problem. The getback function takes the display property, presumably set in the php
script and assigns it to the display dynamic text field instance using the htmlText
property. This means that whatever is assigned is interpreted as HTML text.
The php file must take the data, access and update the appropriate record in the MySql
database, then access all the records (all 4) in the database, format the results, and return
this string of characters to the Flash program. Structured Query Language, SQL, is used
to access the database. The table in the database is named votes. Each record has two
fields (actually, each record has an id field, but that is not used.): candidate and votescast.
In php, variable names start with dollar signs. The setting of the data sent back to the
other program is done using the name you decide on (here, display) and print statements,
starting with print "&display = ";
Here is the code (with my user name, password and database name replaced by question
marks. You will need to change this to values for your account):
<?php
// Incoming Variables..
$data
= $HTTP_POST_VARS['data'];
$host="localhost";
$user="????";
$password="?????";
$DBname="??????";
$link=mysql_connect($host,$user,$password);
print "&display=";
$query = "UPDATE votes SET votescast =
votescast+1 WHERE candidate = '" .$data . "'";
$result = mysql_db_query($DBname,$query,
Delimiter starting
php
comment
Extracts the data
input variable
Specifies that the
database is on
this server
computer
Replace with
allowed user
name
Replace with
password for that
user
Replace with
database name
Establish the link
from php to the
MySql database
Print statement to
start setting of
display
Define the SQL
query: update the
one record with
candidate field
equal to the value
sent over in the
data variable
This statement
$link);
Define another
query, this one
extracting all the
records from the
votes table.
This statement
does the query,
return the results
in the $result
variable. This is
called a recordset.
$result = mysql_db_query($DBname,$query,
$link);
while ($row=mysql_fetch_array($result)) {
"<br>";
print $row["candidate"];
The while
statement uses a
built-in function
in php that
extracts an
associative array
corresponding to
a row in the
$result recordset.
It will return false
when there are no
more rows
(meaning no
more records).
Output a line
break tag.
Output the
element of the
$row array
corresponding to
"candidate"
Output a colon
and a space
Output the
element of the
$row array
corresponding to
"votescast"
Output a line
break
}
?>
This is the application! I wrote two more php scripts (in addition to scripts defining the
table and initializing it with 4 records), none connected to Flash. One reset the values to
zero and the other showed the values.
More on php can be found in my book: Creating Database Web Applications with PHP
and ASP, Charles River Media, publishers.