I have this flash code. It sends score and username in my game to savescores.php. But I have the error above. I have changed URLLoaderDataForma.VARIABLES to TEXT but still same error occurs. What should i do to fix this problem? Thanks in advance..
private function SendScore(score:int)
{
var variables:URLVariables = new URLVariables();
variables.score = score;
variables.username = username;
var urlloader:URLLoader = new URLLoader();
var urlrequest:URLRequest = new URLRequest('http://localhost:90/savescores.php');
urlrequest.method = URLRequestMethod.POST;
urlrequest.data = variables;
urlloader.dataFormat = URLLoaderDataFormat.TEXT;
urlloader.load(urlrequest);
urlloader.addEventListener(Event.COMPLETE, CompleteHandler, false, 0, true);
urlloader.addEventListener(IOErrorEvent.IO_ERROR , ErrorHandler, false, 0, true);
}
private function CompleteHandler(e:Event)
{
var vars:URLVariables = new URLVariables(e.target.data);
if(vars.success) trace('Saving succeeded');
else ('Saving failed');
}
private function ErrorHandler(e:IOErrorEvent)
{
trace('Error occured');
}
ANSWER: Issue was with PHP
-- You must return a var to AS3 or you will get this error.
I am posting this because unless the viewer reads the comments they will not know what the issue was and a solution. Anil, who asked this question stated it was an issue with the PHP and not as3 but gave no reason as to why. It seems the answer marked correct above is not a sufficient answer because it only suggests a possible solution. Where Anil, admitted it was an issue with the php. I believe this answer is more complete and can help someone who has this same issue.
I had a similar issue. The problem was with the php. Unless you output something back from the php you will get this error.
Flash did not like nothing being returned from php and gave the following error:
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at flash.net::URLLoader/onComplete()
Here was a part of my PHP that did not return anything:
if($ConfirmedEmail=="true")
{
echo "Status=true";
}
if($ConfirmedEmail=="false")
{
echo "Status=false";
}
But if it was neither true/false it would OUTPUT NOTHING. = Flash Does Not Like!
So the PHP code had to be this:
if($ConfirmedEmail=="true")
{
echo "Status=true";
}
else if($ConfirmedEmail=="false")
{
echo "Status=false";
}
else
{
echo "Status=Nada";
}
Here is my AS3 code for you to look at. Hope it helps someone.
public function checkEmail(e:Event = null)
{
var urlreq = new URLRequest("http://www.MyWebsite.com/myScript.php");
urlreq.method = URLRequestMethod.POST;
var urlvars = new URLVariables();
urlvars.userID = Main.userID;
urlreq.data = urlvars;
var loader:URLLoader = new URLLoader(urlreq);
loader.addEventListener(Event.COMPLETE, completed);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlreq);
}
public function completed(event:Event):void
{
var variables:URLVariables = new URLVariables(event.target.data);
trace("Email Confirmed: " + variables.Status);
}
I assume that the problem is not with request, but with response handling. You are instantiating vars as URLVariables, but probably e.target.data does not comply with the format expected. Trace e.target.data value to get more information.
Related
I'm just trying out PHP and AS3 in Flash CS6. I want a few strings to be passed from PHP to Actionscript3 and store them in an array. Is this possible?
Currently I'm doing this. My PHP code is
<?php
echo "one,two,three,four";
?>
and AS3 code is:
var myRequest:URLRequest = new URLRequest("please7.php");
var myLoader:URLLoader = new URLLoader();
myLoader.load(myRequest);
myLoader.dataFormat = URLLoaderDataFormat.TEXT;
myLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(event:Event):void {
var variables:String = event.target.data;
trace(variables);
var arr1:Array = new Array();
arr1 = variables.split(",");
trace(arr1);
}
This gives me this output:
<?php
echo "one,two,three,four";
?>
If I use myLoader.dataFormat = URLLoaderDataFormat.VARIABLES; instead of myLoader.dataFormat = URLLoaderDataFormat.TEXT; ..I'm getting this error
Error: Error #2101: The String passed to URLVariables.decode() must be
a URL-encoded query string containing name/value pairs.
I want the output to be a simple [one two three four]
What am I doing wrong?
You can do like following, it will be passed as a query-string
In PHP
<?php
echo 'q=' . htmlentities($your_data);
?>
In AS3
var urlLoader:URLurlLoader = new URLurlLoader();
urlLoader.dataFormat = URLurlLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, onComplete);
urlLoader.load(new URLRequest("somefile.php"));
function onComplete (event:Event):void {
trace (urlLoader.data.q.toString());
}
I'm trying to pass some variables from php to flash, im using this actionscript code:
public function gameOver(score:Number)
{
totalScore.text = score.toString();
var scriptVars:URLVariables = new URLVariables();
scriptVars.score = score;
var scriptLoader:URLLoader = new URLLoader();
var scriptRequest:URLRequest = new URLRequest("checkScores.php");
scriptRequest.method = URLRequestMethod.POST;
scriptRequest.data = scriptVars;
scriptLoader.load(scriptRequest);
scriptLoader.addEventListener(Event.COMPLETE, handleLoadSuccessful);
scriptLoader.addEventListener(IOErrorEvent.IO_ERROR, handleLoadError);
}
function handleLoadSuccessful(e:Event):void
{
trace("Scores Loaded");
var vars:URLVariables = new URLVariables(e.target.data);
nickname1.text = vars.nickname;
score1.text = vars.score;
}
function handleLoadError($evt:IOErrorEvent):void
{
trace("Load failed.");
nickname1.text ="error";
}
And this php code:
<?php
... some code for the mysql connection and select sentence ...
$topScores = mysqli_query($con, $topScores);
$topScores = mysqli_fetch_array($topScores);
echo "&nickname=$topScores[nickname]&score=$topScores[score]";
?>
both runs without errors, the problem is that what i get on flash aren't the variables values but the name of the variables, in other words what i get on vars.nickname is
$topScores[nickname]
and for vars.score
$topScores[score]
If i run the php alone i get this:
&nickname=jonny&score=100
which are the actual variable values i'm trying to get, any help would be greatly appreciated.
I think you may just be loading up the php file as a text file from flash. Can you change the following line:
new URLRequest("checkScores.php");
to something like:
new URLRequest("http://localhost/checkScores.php");
or whatever you see in the browser address bar when you "run" it as you said in your question.
I'm trying to work out from an old as2 tutorial how to amend a script for as3/php eCard system for a business card but I can't find reference anywhere to how you'd do the following :
AS2 :
loadVariablesNum ("http://www.theSite.com/Cards/bCard/"+BcardText+".txt", 0);
AS3 :
// setup URLLoader
var loader:URLLoader = new URLLoader;
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
// event listener for function when loaded
loader.addEventListener(Event.COMPLETE, varsLoaded);
// file URLRequest
loader.load(new URLRequest("http://www.theSite.com/Cards/bCard/"+BcardText+".txt"));
// set the variables from the data.txt file
function varsLoaded (event:Event):void {
//Load Data
cName.text = loader.data.cName;
cDescription.text = loader.data.cDescription;
}
With this it kicks out the following error message :
Error opening URL 'http://www.theSite.com/Cards/bCard/undefined.txt'
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at flash.net::URLLoader/onComplete()
I can't work out where or how you define the +BcardText+ for it to pull it in.
Any help would be gratefully received.
I'm not sure If I'm even close as its from as2, it seems the logical approach for loading it but I've not dealt with external files having parameters before.
Thanks in advance if anyone can help out in anyway!
NEW LOADER - FIXED!!!
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://www.theSite.com/Cards/bCard/"+BcardText+".txt");
loader.load(request);
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
function loaderIOErrorHandler(event:IOErrorEvent):void{
trace("ioErrorHandler: " + event);
}
// set the variables from the .txt file
function completeHandler (event:Event):void {
//trace("Content: " + loader.data);
this.Variable1.text = loader.data.Variable1; //Whatever dataField1 you saved as
this.Variable2.text = loader.data.Variable2; //Whatever dataField2 you saved as
}
Then you just setup FlashVars to distinguish the +BcardText variable in the loader prior to committing it!
BcardText was a variable defined in the as2 project. Look around (maybe on previous frames?) and you should find where it got declared. It looks like an ID to represent the card. So each card has a unique file 12345.txt, 09876.txt etc.
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://www.theSite.com/Cards/bCard/"+BcardText+".txt");
loader.load(request);
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
function loaderIOErrorHandler(event:IOErrorEvent):void{
trace("ioErrorHandler: " + event);
}
// set the variables from the .txt file
function completeHandler (event:Event):void {
//trace("Content: " + loader.data);
this.Variable1.text = loader.data.Variable1; //Whatever dataField1 you saved as
this.Variable2.text = loader.data.Variable2; //Whatever dataField2 you saved as
}
Then you just setup FlashVars to distinguish the +BcardText variable in the loader prior to committing it!
Have scoured the Internet and gone half mad working this out but finally chuffed to say I broke it's back...!
A big thanks for helping point me in the right direction guys. #Jason #Eugen
Θ)
I have this flash code. It sends score and username in my game to savescores.php. But I have the error above. I have changed URLLoaderDataForma.VARIABLES to TEXT but still same error occurs. What should i do to fix this problem? Thanks in advance..
private function SendScore(score:int)
{
var variables:URLVariables = new URLVariables();
variables.score = score;
variables.username = username;
var urlloader:URLLoader = new URLLoader();
var urlrequest:URLRequest = new URLRequest('http://localhost:90/savescores.php');
urlrequest.method = URLRequestMethod.POST;
urlrequest.data = variables;
urlloader.dataFormat = URLLoaderDataFormat.TEXT;
urlloader.load(urlrequest);
urlloader.addEventListener(Event.COMPLETE, CompleteHandler, false, 0, true);
urlloader.addEventListener(IOErrorEvent.IO_ERROR , ErrorHandler, false, 0, true);
}
private function CompleteHandler(e:Event)
{
var vars:URLVariables = new URLVariables(e.target.data);
if(vars.success) trace('Saving succeeded');
else ('Saving failed');
}
private function ErrorHandler(e:IOErrorEvent)
{
trace('Error occured');
}
ANSWER: Issue was with PHP
-- You must return a var to AS3 or you will get this error.
I am posting this because unless the viewer reads the comments they will not know what the issue was and a solution. Anil, who asked this question stated it was an issue with the PHP and not as3 but gave no reason as to why. It seems the answer marked correct above is not a sufficient answer because it only suggests a possible solution. Where Anil, admitted it was an issue with the php. I believe this answer is more complete and can help someone who has this same issue.
I had a similar issue. The problem was with the php. Unless you output something back from the php you will get this error.
Flash did not like nothing being returned from php and gave the following error:
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at flash.net::URLLoader/onComplete()
Here was a part of my PHP that did not return anything:
if($ConfirmedEmail=="true")
{
echo "Status=true";
}
if($ConfirmedEmail=="false")
{
echo "Status=false";
}
But if it was neither true/false it would OUTPUT NOTHING. = Flash Does Not Like!
So the PHP code had to be this:
if($ConfirmedEmail=="true")
{
echo "Status=true";
}
else if($ConfirmedEmail=="false")
{
echo "Status=false";
}
else
{
echo "Status=Nada";
}
Here is my AS3 code for you to look at. Hope it helps someone.
public function checkEmail(e:Event = null)
{
var urlreq = new URLRequest("http://www.MyWebsite.com/myScript.php");
urlreq.method = URLRequestMethod.POST;
var urlvars = new URLVariables();
urlvars.userID = Main.userID;
urlreq.data = urlvars;
var loader:URLLoader = new URLLoader(urlreq);
loader.addEventListener(Event.COMPLETE, completed);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlreq);
}
public function completed(event:Event):void
{
var variables:URLVariables = new URLVariables(event.target.data);
trace("Email Confirmed: " + variables.Status);
}
I assume that the problem is not with request, but with response handling. You are instantiating vars as URLVariables, but probably e.target.data does not comply with the format expected. Trace e.target.data value to get more information.
i have a php script that print with echo this:
'&string="tom,dick,harry"'
and i need to put the "tom,dick,harry" in an actionscript string, that i have to split in an array. I'm having problems reading the php output, i'm using the URLLoader and TheURLVariables Classes in this way
var myRequest:URLRequest = new URLRequest("ip/directory/script.php");
var myLoader:URLLoader = new URLLoader();
function onLoaded(event:Event):void {
var variables:URLVariables = new URLVariables( event.target.data );
modelli = variables.string.split(",");
caricaColori(modelli[0]);
}
myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myLoader.load(myRequest);
What am i doing wrong?
Thought that you got problem with URLVariables, I still not totally understand this yet
function onLoaded(event:Event):void {
var variables:URLVariables = new URLVariables( event.target.data );
modelli = variables.string.split(",");
caricaColori(modelli[0]);
}
why do you need to store it into a URLVariables instance? Why don't parse it directly. If you are afraid of the "&string=", you don't need to echo it on PHP side, or can slice it out on Actionscript side.
modelli = event.target.data.split(",");
Perhaps using a variable name that isn't reserved might help. string seems like a bad choice.
// in php change
echo '&string="tom,dick,harry"'
// to
echo "tom,dick,harry"
// in actionscript change
function onLoaded(event:Event):void {
var str:String = event.target.data;
modelli = str.split(",");
caricaColori(modelli[0]);
}
If you want to add more variables and whatever, I would suggest turning the php response into an xml file. URLVariables should be used to SEND data to the server not for parsing a server response.
LOOK HERE
function onLoaded(event:Event):void {
var variables:URLVariables = new URLVariables( event.target.data );
modelli = variables.string.split(",");
caricaColori(modelli[0]);
}
Your problem is that you're loading the variables into a URLVariables container, and then trying to call a string function on it. I would do it this way instead:
function onLoaded(event:Event):void {
//load data as a string
var variables:String = event.target.data;
//make a new array
var modelli:Array = new Array();
modelli = variables.split(",");
//possibly pop the array
modelli.pop(); //pop off the last empty element of array
caricaColori(modelli[0]);
}
There's also a good chance that when you load this PHP data, you'll need to pop() the last element off of the array because it will be an empty string.