views:

135

answers:

2

Hi,

A project of mine involves a flash movie (.swf) in a webpage, where the user has to pick from a number of items, and has the option to thumbs up or thumbs down (vote on) each item.

So far I have gotten this to work during each run of the application, as it is currently loading the data from an XML file - and the data is still static at the moment.

I need to persist these votes on the server using a database (mySQL), such that when the page is reloaded, the votes aren't forgotten.

Has anyone done this sort of thing before?

The two mains methods that I have found on the 'net are

  1. either direct communication between AS3 and the SQL using some sort of framework, or
  2. passing the SQL query to a PHP file, which then executes the SQL query and returns the SQL to AS3.

Which of these methods is the better option?

For the latter method (involving PHP), I have been able to find resources on how to acheive this when attempting to retrieve information from the database (i.e. a read operation), but not when attempting to send information to the database (i.e. a write operation, which is needed when the users vote). How is this done?

Thank you!


Edit: Implemented solution

Somewhere in the PHP file:

if ($action == "vote")
{
 $id = $_POST['id'];
 $upvotes = $_POST['upvotes'];
 $query = "UPDATE `thetable` SET `upvotes` = '$upvotes' WHERE `thetable`.`id` = '$id' LIMIT 1 ;";
 $result = mysql_query($query);
}

Somewhere in the ActionsScript:

 public function writeToDb(action:String)
 {
  var loader:URLLoader = new URLLoader();
  var postVars:URLVariables = new URLVariables();
  var postReq:URLRequest = new URLRequest();

  postVars.action = action;
  postVars.id = id;
  postVars.upvotes = upvotes;

  postReq.url = <NAME_OF_PHP_FILE>;
  postReq.method = URLRequestMethod.POST;
  postReq.data = postVars;

  loader.load(postReq);
  loader.addEventListener(Event.COMPLETE, onWriteToDbComplete);
 }
+2  A: 

I am not aware of any framework that supports method-1.

I would use method-2 - but instead of making the query within Flash and passing it to PHP, I would rather pass the related data and construct the query in PHP itself. This is safer because it is less susceptible to SQL injection attacks.

This answer has an example of sending data from flash to the server - it talks about ASP, but the method is same for PHP (or any technology) - just change the URL.

Within the php code, you can read the sent data from the post $_POST (or $_GET) variable.

$something = $_POST["something"]
Amarghosh
@Amargosh Thank you! +1 for constructing query in PHP, and +1 for your other answer as well, cos it worked for me!
bguiz
+1  A: 

Many different options:

AMFPHP - binary messaging format between PHP and Actionscript/Flash.

LoadVars - for POSTing and GETing values to a PHP script.

JSON - Using the AS3Corelib you can post JSON formatted data to your web site (just like an AJAX script does).

pygorex1