Question

I've got a problem that's either insanely simple or complex, it's up to you to find out. I've been working on trying to incorporate the URL Loader class into the beginners graphics program - Stencyl. I am fluent in HTML, CSS and PHP but actionscript is completely new to me so I really could use with a hand. Here's what I've got: There are 4 files hosted on my domain:

Webpage.html

Stylesheet.css

RequestData.php

FlashDoc.swf

The html and css code is simple, no problems there and the swf file embed in the html document. The flash file is a simple form with a text field, submit button and two dynamic text fields. The code goes as follows:

// Btn listener
submit_btn.addEventListener(MouseEvent.CLICK, btnDown);
// Btn Down function
function btnDown(event:MouseEvent):void {


// Assign a variable name for our URLVariables object
var variables:URLVariables = new URLVariables();
// Build the varSend variable
// Be sure you place the proper location reference to your PHP config file here
var varSend:URLRequest = new URLRequest("http://www.mywebsite.com/config_flash.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
// Build the varLoader variable
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);

variables.uname = uname_txt.text;
variables.sendRequest = "parse"; 
// Send the data to the php file
varLoader.load(varSend);

// When the data comes back from PHP we display it here 
function completeHandler(event:Event):void{

var phpVar1 = event.target.data.var1;
var phpVar2 = event.target.data.var2;

result1_txt.text = phpVar1;
result2_txt.text = phpVar2;

} 


}

I then have a small PHP file with this code:

<?php
// Only run this script if the sendRequest is from our flash application
if ($_POST['sendRequest'] == "parse") {
// Access the value of the dynamic text field variable sent from flash
$uname = $_POST['uname'];
// Print  two vars back to flash, you can also use "echo" in place of print
print "var1=My name is $uname...";
print "&var2=...$uname is my name.";

}

?>

This is, for some reason, not working. The result is just two blank text fields and being an actionscript noob, I have no idea what is up. Any help would be greatly appreciated. Thank you for your time.

Was it helpful?

Solution

The answer to your issue is both simple and surprising, if you're not used to AS3.

In AS3, the flash.* classes tend to, when a setter is used, make and store a copy of the passed object. Since they store a copy, any modification on the original instance after the setter isn't applied on the copy, and thus is ignored.

It is the case of, for example, DisplayObject.filters, ContextMenu.customItems or URLRequest.data.

In your code, you are setting varSend.data = variables before filling variables. You should do the reverse :

variables.uname = uname_txt.text;
variables.sendRequest = "parse"; 
varSend.data = variables;
// Send the data to the php file
varLoader.load(varSend);

Only some classes do that, and even then, they usually don't do it with all of their setters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top