Search code examples
phpactionscript-3flashurlloader

Flash ActionScript 3.0 URL connection only works when being tested and fails when published


I have a program(as 3.0 .swf) that takes a users name, email and the desired information they want to receive. It then creates an email and passes these variables to my server where a php file sends it to the email the user entered. When I test the program in flash it works great. The email goes right through. But when I export it the program is unable to connect. I have the setting to "access network only." I have published an AIR and html version as well and neither work. I have spent 2 and a half hours searching and have found people with the same problem but no answers. This is the only thing stopping me from launching the program!

Heres my code:

import flash.events.Event;

//---------------Setup variables
var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://myserver/phpfile.php");


var variables:URLVariables = new URLVariables();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
req.method = URLRequestMethod.POST;
var Info:String = "";
//tab index
txName.tabIndex = 0;
txEmail.tabIndex = 1;
//listener fot textfield changes
txName.addEventListener(Event.CHANGE, txErrorCheck);
txEmail.addEventListener(Event.CHANGE, txErrorCheck);
function txErrorCheck(event:Event):void {
if (txError.text.length>0) {
    txError.text = "";
}
}
SEND.addEventListener(MouseEvent.CLICK, sendForm);
function sendForm(evt:MouseEvent):void {
if (txName.text.length<=0) {
    txError.text = "Please Enter A Name";
} else if (!txEmail.text.length || txEmail.text.indexOf("@") == -1 ||        txEmail.text.indexOf(".") == -1) {
    txError.text = "Please Enter A Valid Email!";
} else {
    mcLoader.gotoAndPlay(2);
    variables.senderName = txName.text;
    variables.senderEmail = txEmail.text;
    variables.Info = Info;
    req.data = variables;
    loader.load(req);
    loader.addEventListener(Event.COMPLETE, receiveLoad);
}
}
function receiveLoad(evt:Event):void {
if (evt.target.data.retval == 1) {
    mcLoader.gotoAndStop(25);
} else {
    mcLoader.gotoAndStop(1);
    txError.text="**  SERVER ERROR **";
}
}
//Reset form
function resetForm(evt:MouseEvent):void {
txName.text="";
txEmail.text="";
}
stop();

Solution

  • Have a look at adobe flash security polocies. You should have a cross-domain xml file in the root of your server. In debug mode it always works because security is skipped.

    Take a look at this post AS3 | SWF load issue or google about using cross-domain.xml files inside flash.