Thursday, October 3, 2013

AS2 (actionscript 2) Post final score in flash to MySQL databse using PHP


The following actionscript 2 post the data string to sendtest.php and display the feedback result.
Example, we get the scoreTotal  value in flash and want to post it in MySQL database, AS2 script in flash:
 scoreTotal = 100;
var php_process:LoadVars = new LoadVars();
var post_variable:LoadVars = new LoadVars();
    post_variable.string = scoreTotal;
    post_variable.sendAndLoad("http://www.cesei.org/sendtest.php",php_process,"POST");

php_process.onLoad = function(success:Boolean) {
    if (success) {
        my_txt.text +=  php_process.result;
        my_txt.setTextFormat(my_fmt);
    }
    else {
        my_txt.text += "Error connecting to server.";
        my_txt.setTextFormat(my_fmt);
    }
};

my_txt.setTextFormat(my_fmt);
 

  sendtest.php: we need to use "result=" in echo so the feedback can be displayed in flash php_process.result, simple test:

 <?php
if(isset($_POST[string])){
    $upper = strtoupper($_POST[string]);
    echo "result=Your average score $upper".' is posted in CESEI database.';
}
else{
    echo "result=empty string posted";   
}

?>

if it is needed to be  connected to MySQl database:
<?php 
 $host = "localhost";
 $user = "root";
$password = "";
$database = "wrapup";
$link = mysql_connect($host, $user, $password); mysql_select_db($database);
if(isset($_POST[string])){
    $score = mysql_real_escape_string($_POST[string]);
    $addClient = "INSERT INTO data (studentscore) VALUES ('$score')";
   if(mysql_query($addClient) )  echo "result=Your average score $upper".' is posted in CESEI database.';
    else die(mysql_error());
    mysql_close($link); }
else{
   echo "result=empty string posted";
}
?>

No comments:

Post a Comment