AS3 Cannot communicate with php.

Hi,

I have spent the whole day getting this seemingly simple piece of code to work but it stubbornly refuses to.

var variables:URLVariables = new URLVariables();	

var varSend:URLRequest = new URLRequest("comm.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;


var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

	variables.user1 = "Guest";
	variables.user2 = "Newbie";
	variables.sendRequest = "parse";
	
	// send data to php
	varLoader.load(varSend);
	varLoader.addEventListener(Event.COMPLETE, completeHandler);

	function completeHandler(event:Event):void
	{
		trace(event.target.data.var1);
		var var1 = event.target.data.var1;
		var var2 = event.target.data.var2;
		var var3 = event.target.data.var3;
	}
        // Three textboxes on stage.
	str1.text = var1;
	str2.text = var2;
	str3.text = var3;
	

The php code is as follows

    ($_POST['sendRequest'] ==="parse"){`

	// var_dump($_POST);
	
	$user1 = $_POST['user1'];
	$user2 = $_POST['user2'];
	
	$var1 = "Box of";
	$var2 = "chocolates";
	$var3 = "is packed";
	
	echo "&var1=".$var1;
	echo "&var2=".$var2;
	echo "&var3=".$var3;
	
	exit();
    }

I tried numerous formatting of return strings like ( as in many different examples on google)

echo “var1=$var1”;,
echo “var1 =”.$var1;.
echo “&var1 =”.$var1;.
.
.

and so on,
another, all in one string as below
returnString = “&var1=”.$var1."&var2=".$var2."&"; and whatever I could think of.
(this worked in AS2 sendAndLoad very well but here I am grappling with AS3.)

I keep getting the following error.

TypeError: Error #2007: Parameter text must be non-null.
	at flash.text::TextField/set text()
	at AS3_swf_php_comm_fla::MainTimeline/AS3_swf_php_comm_fla::frame1()

Please help !!

Thanks all !

You have to set your .text properties in completeHandler. Your var variables only exist there, and aren’t defined until after the php variables load. You know when it loads within completeHandler.

Thanks Senocular,

You are right but I had tried by defining the variables var1, var2 and var3 at the very beginning of the program ( lines 1,2 and 3) and then shifting the .text variables outside. Nogo.

I have also tried by keeping them within the completeHandler function but the result was the same.

However I will try it all once again and revert.

Thank you.

Hi Senocular !!

I’ld be ■■■■■■ !! It worked ! I swear I tried this tens of time during the day and it did not work. I guess you have the midas touch !!

Thanks a ton.

Its not just scope, its also timing. Putting the vars at the top of the page would fix the scope (access to use the variables) but not the timing (the point in time in which they’re defined).

The reasons callbacks are used in things like URLLoader.load() is because the loading of a remote document takes time, and other code shouldn’t have to wait for that load to happen to keep running. So instead of halting all code that could run during a load, that code is allowed to run and the code that needs to react to the completion of the load is contained within a callback that gets executed when the document loaded.

Anything in the same frame outside of the callback gets run before the callback, even if its written after the callback.

// RUNS FIRST ...

var variables:URLVariables = new URLVariables();	
var varSend:URLRequest = new URLRequest("comm.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
variables.user1 = "Guest";
variables.user2 = "Newbie";
variables.sendRequest = "parse";
varLoader.load(varSend);
varLoader.addEventListener(Event.COMPLETE, completeHandler);

function completeHandler(event:Event):void
{

    // RUNS THIRD, after all other code already ran ...

    trace(event.target.data.var1);
    var var1 = event.target.data.var1;
    var var2 = event.target.data.var2;
    var var3 = event.target.data.var3;
}

// RUNS SECOND (or as part of FIRST) ...
// vars have _not_ been set to any values yet

str1.text = var1;
str2.text = var2;
str3.text = var3;

Hi Senocular,

Its not just scope, its also timing. Putting the vars at the top of the page would fix the scope (access to use the variables) but not the timing (the point in time in which they’re defined).

Yes that occurred to me too, after I had just posted the last reply and was going through it. But thanks for the further clarification all the same. Much obliged !!:slightly_smiling_face:

Thank you