Skip to main content
Known Participant
June 22, 2010
Answered

checking success of data sent to php

  • June 22, 2010
  • 2 replies
  • 1035 views

Hi Everyone,

I'm trying to send some variables to a php script, and I don't know how to figure out if I'm successful or not.  Here's the AS 3 code I have now (the code snippet assumes that myArray was defined earlier):

function sendData():void {

     var sendToPHP:URLVariables = new URLVariables;

     // mode is a variable in the PHP script that tells the script what to do

     sendToPHP:URLVariables.mode = "add_go";

     sendToPHP:URLVariables.myData = myArray.toString();

     var myURLRequest:URLRequest = new URLRequest("http://www.someDomain.com/index.php");

     myURLRequest.data = sendToPHP;

     navigateToURL(myURLRequest, '_blank');

}

I'm completely new to sending data outside of Flash, so I hope I'm not too far off track.  I'm not a PHP developer either, I just want to know if there's a way to add an event complete listener that might tell me if this transaction was successful or not.

Thanks in advance!

-Dave

This topic has been closed for replies.
Correct answer Darshan_Rane

//Sending data to php

var request:URLRequest = new URLRequest("your url"); 

var loader:URLLoader = new URLLoader(); 

loader.dataFormat = URLLoaderDataFormat.VARIABLES; 

request.data = "your variables"; 

request.method = URLRequestMethod.POST; 

loader.addEventListener(Event.COMPLETE, handleComplete); 

loader.load(request);

//This gets values from php

private function handleComplete(event:Event):void

     var loader:URLLoader = URLLoader(event.target);

     var str = loader.data.result;

     //str variable stores return value from php variable "result"

     if (str)

     {

          "Perform some action"

     }

2 replies

Darshan_Rane
Darshan_RaneCorrect answer
Inspiring
June 23, 2010

//Sending data to php

var request:URLRequest = new URLRequest("your url"); 

var loader:URLLoader = new URLLoader(); 

loader.dataFormat = URLLoaderDataFormat.VARIABLES; 

request.data = "your variables"; 

request.method = URLRequestMethod.POST; 

loader.addEventListener(Event.COMPLETE, handleComplete); 

loader.load(request);

//This gets values from php

private function handleComplete(event:Event):void

     var loader:URLLoader = URLLoader(event.target);

     var str = loader.data.result;

     //str variable stores return value from php variable "result"

     if (str)

     {

          "Perform some action"

     }

Davey_J_Author
Known Participant
June 23, 2010

Thanks to both of you for your quick responses.  I am getting a response from the server in Flash now.

I do have a couple more questions:

1.  Is there a difference in using:

     loader.dataFormat = URLLoaderDataFormat.VARIABLES

     vs.

     loader.dataFormat = URLLoaderDataFormat.TEXT

The reason I ask is that I get an error about value pairs if I use the VARIABLES option.  I've also seen some debate on this and wanted to know if one worked better over the other.

2.  Do the variable names need to match what is being received in the PHP script?  The reason I'm asking is that the PHP script (written by someone else) is looking for two variables, mode and input.  Both of those are keywords in Flash and I wondered if they could be what is causing my problem on the PHP side.

Thanks!

-Dave

kglad
Community Expert
Community Expert
June 23, 2010

variables are variable/value pairs and text is a string.

to transmit keywords:


urlvariableinstance["mode"]=whatever;

urlvariableinstance["input"]=whateverelse;

kglad
Community Expert
Community Expert
June 22, 2010

you should be using the urlloader class to call your php script and then you can use its complete listener to handle data returned from your php file (or just determine when the php file has completed execution).