Building the Back End of a Content Management System for Flash - 2. The PHP Code
(Page 3 of 4 )
Now that we've created our basic form, we should write the PHP to process the information. For now, we'll call the script "backend.php," which we referred to in our HTML form action, using the post method.
First, after we encapsulate our PHP with the <?php and ?> tags, we will need to write the data.
if (isset($_POST['submit'])) { $content1_txt = $_POST['content1_txt']; |
Upon hitting submit our form action is POST; we need to post the contents of the variable, "content1_txt", giving its contents over to the new posted variable, "content1_txt".
Now, we have to open the text file to which we will be posting the data. If we cannot, we need to say so.
$fp = fopen("data.txt","w+"); if(!$fp) { echo 'Error, the file could not be opened or there was an error creating it.'; exit; }else { echo 'Your content has been saved.'."n"; } |
We first define the $fp variable as set to open the data file which we want for reading and writing, or "w+," as defining it first provides a shortcut of sorts for the next line. The first line tells the server to open the file we want, which is our data.txt file, by using the "fopen()" function. The fopen() function is used to open files in PHP. The w+ mode allows for read and/or write. This mode opens and clears the contents of the file, or creates a new file if it doesn't exist. If we only wanted to read the file, then it would be "r." But because we want to write data to this file, we need to tell the server to do so.
The next section of code means that, if the file is not able to be opened, it will return the error message, and exit; otherwise, return a statement that the file has been opened and the content can be saved. Remember our defined variable from the first line? In PHP, the exclamation mark means "not." So we are saying, in short, if our variable is NOT as we defined it earlier, then do this, which is, in this case, provide the error message and exit. But if it does match, then the server disregards this line and moves on to the next.
Next, we need to put the data entered into the form into the file we just opened or created, then actually write the data.
fwrite ($fp, "content1_txt="); fwrite ($fp, $content1_txt."n"); |
So we use the fwrite() function. We also need to tell the server WHAT to write to the file, and in this case we need the contents of our variable written, so we include that between the parentheses of our function.
Wait a second -- it looks like we've attempted to write the variable twice! Look closer. We only need one variable's contents written to the file. However, in order for Flash to know what to send to the dynamic area of our movie, we need to make sure that the actual variable name is written in the file so that Flash can identify it. (We will show this specifically when we look at the data file in the next section). So the first line is basically writing the variable name used by Flash. The second line is writing the data from the form.
Now, I apologize if I've made this a bit confusing. For simplicity's sake, I gave the variable from the HTML form the same name as the Flash dynamic variable. But we could have easily named them something different. So if I'd called the form variable "text" and the dynamic variable "flash," then our HTML form code would have looked like this:
<textarea name="text" cols="40" rows="10"> </textarea> |
And the PHP would look like more this:
fwrite ($fp, "flash="); fwrite ($fp, $text."n"); |
The great thing about variables is that you can name them anything you like, as long as you remember that you have to call it whatever you named it.
NOTE: Odds are that you are going to have more than one dynamic variable in your Flash. Flash requires that you prefix your variables with the ampersand sign (&) in any external source so that Flash can distinguish what is a variable and what isn't when there are multiple variables. So in ActionScript code, I should use &content1_txt instead of content1_txt; however since I am only using one variable in our demonstration, we are okay without it. Moving on...
echo 'This is what you submitted:'."n"; readfile("data.txt"); |
This next part of the code is really not necessary for the script to work. But if you want to view what you have just entered without having to download or view your data.txt file, then you may want to leave this part in. The next part of the code instructs the server to read the contents you've just written and echo that back to the browser. You could use a few different commands or functions to read the file contents and echo them back, as the "readfile" command doesn't use any formatting at all. But because that is not the focus of this tutorial, we don't need any formatting of the read data for now.
Finally, we must close the file.
In summary, here's what your PHP code in backend.php might look like all together:
<?php //write the data if (isset($_POST['submit'])) { $content1_txt = $_POST['content1_txt']; //open the text file $fp = fopen("data.txt","w+"); if(!$fp) { echo 'Error, the file could not be opened or there was an error creating it.'; exit; }else { echo 'Your content has been saved.'."n"; } //put data from the form into the text file fwrite ($fp, "content1_txt="); fwrite ($fp, $content1_txt."n"); //read back the text file data echo 'This is what you submitted:'."n"; readfile("data.txt"); //close the file fclose($fp); } ?> |
3. The Flat File Database
As I've shown in the previous element, we need to store the data that we asked PHP to post from our form. Remember the ActionScript we wrote in the first tutorial? We referred to "data.txt" as our source from which to load our variables into Flash, as we also did in backend.php. This is our flat file database. It is nothing more than a simple text file.

Notice that we have our variable "content1_txt" in there, as we asked PHP to echo for us, so that Flash can identify the variable, and know it's the correct one to place in our dynamic area. You need to make sure that there are no spaces between the variable, the "=" sign and the content you wish to plug into Flash. This is important. (As mentioned above in my update, the data.txt variables, when there are multiple variables, should always begin with the ampersand sign (&)).
Next: Gluing it All Together >>
More Flash Articles
More By Jennifer Sullivan Cassidy