PDA

View Full Version : Why is my as3 to php not working



chumps
February 3rd, 2009, 09:38 AM
I have studied this again and again trying to find results. Traced out all the variables before they leave the flash file, and then again when they enter the php. In this space they seem to get lost. I have no idea why this is:
Flash Code and PhP code shown below:

Flash Code:

package {

import flash.display.*;
import flash.net.*;
import flash.events.Event;

public class sendPracticeResults extends Sprite {

public var _totalAttempts:Number;
public var _totalCorrect:Number;
public var _userId:Number;
public var _gameId:Number;
public var resultText:String;
public var urlrequest:URLRequest = new URLRequest( "http://liberation.iwishiwaschucknorris.com/games/php/practiceResults.php" );


public function sendPracticeResults(totalAttempts:Number, totalCorrect:Number, userId:Number, gameId:Number) {

_totalAttempts = totalAttempts;
_totalCorrect = totalCorrect;
_userId = userId;
_gameId = gameId;




var variables:URLVariables = new URLVariables();
variables.attempts = _totalAttempts;
trace("variables.attempts: "+variables.attempts);
variables.correct = _totalCorrect;
variables.userId = _userId;
variables.gameId = _gameId;

urlrequest.method = URLRequestMethod.POST;
urlrequest.data = variables;

sendToURL(urlrequest);
checkResult();
}

private function checkResult() {
var loader:URLLoader = new URLLoader (urlrequest);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlrequest);
}

private function onComplete (event:Event):void{
var variables:URLVariables = new URLVariables( event.target.data );

resultText = variables.msg;
//_returnText = new resultTangle(""+resultText+"", 100, 60, 0xFF0000, _mainClass);
//addChild(_returnText);
trace(resultText);

}

}

}

PHP code:

<?php

include("connect.inc.php");

$attempts = $_POST['attempts'];
$correct = $_GET['correct'];
$userId = $_GET['userId'];
$gameId = $_GET['gameId'];
echo("Attempts Goes Here".$attempts);

function init() {
if(isset($attempts) && isset($correct) && isset($userId) && isset($gameId)){
insertIntoTable($attempts, $correct, $userId, $gameId);
}
else
{
printOutput("0", "Fail");

}
}//close function

function insertIntoTable($attempts, $correct, $userId, $gameId) {
$sql = "INSERT INTO adding ( Game_ID, game_Type, adding_Score) VALUES ('$gameId', '1', '$correct')";
$result = mysql_query($sql);
if($result) {
printOutput("1", "Score Uploaded");
}
else
{
printOutput("0", "Failed to Upload");
}



}//Close function


function printOutput($code, $msg){
print "par=$code&msg=$msg";
}//close function






init();
?>

any help will be greatly appreciated.

Greggeh
February 3rd, 2009, 11:12 AM
urlrequest.method = URLRequestMethod.POST;

$attempts = $_POST['attempts'];
$correct = $_GET['correct'];
$userId = $_GET['userId'];
$gameId = $_GET['gameId'];

Using GET variables in your php while you use POST in your flash is nog going to work.
Change your GET variables to POST variables.

Yannick

cbeech
February 3rd, 2009, 11:13 AM
i *think* that the other 3 variables in the php should be $_POST[x] also rather than $_GET

which message are you getting from the php errors messages?

EDIT: sorry Greggeh - I was still reading/typing lol

chumps
February 3rd, 2009, 04:08 PM
cheers guys, that was really dumb of me to not notice that.
I fixed it in the mean time and slapped my forehead when i realised how dumb i had been.