Skip to main content
Inspiring
May 4, 2008
Question

PHP+AS3 Problem

  • May 4, 2008
  • 6 replies
  • 1055 views
I am trying to send data (variables) to a php file. I keep getting an error even though I am using the exact method used in Flash Help (something like Using External Data).
This topic has been closed for replies.

6 replies

jishi9Author
Inspiring
May 7, 2008
Ok I have this code now (I made the php code as simple as possible so that I can trace the source of the problem easily :




////////////AS3

var req:URLRequest = new URLRequest();
req.url = "save.php";
req.method = URLRequestMethod.POST;
var vars:URLVariables = new URLVariables();
req.data = vars;

var loader:URLLoader = new URLLoader();
trace("begin sending...");
try {
loader.load(req);
} catch (error:Object) {
ttrace(error);
}
loader.addEventListener(Event.COMPLETE, done);


function done(event:Event) {
trace(event.target.data);
}

stop();


//////////////PHP Code

<?php
echo "HI";
?>



/////////Output Panel:

begin sending...
<?php



echo "HI";



?>

////////////////////////////////////////////////////////////////////////DONE

Why am I getting the php file back and not 'HI' from the echo statement?

jishi9Author
Inspiring
May 6, 2008
Ok I think I understand how I can get this to work. I keep the dataFormat as Text and then I create a new URLVariables which decodes the data received from the php file, which seems to work fine, except for one thing. The data I receive is the actual contents of the php file, not its echo statements. It seems as if the php file is not working.I even changed the php file to simply echo a string, but with no change.

I get this problem both locally and on the server which is php enabled.

Any ideas?
jishi9Author
Inspiring
May 6, 2008
When I remove this line, it seems to send (I get an OK in my textfield) but I don't think the php code correctly receives the data (it expects variables to be sent to it) and I also get an error regarding the writing variable:

ReferenceError: Error #1069: Property writing not found on String and there is no default value.
at this_fla::MainTimeline/done()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()


I think it is necessary to have the DataFormat set to variables in this situation.
Participating Frequently
May 6, 2008
Remove:

loader.dataFormat = URLLoaderDataFormat.VARIABLES;

This may not solve the problem you are having, but I have seen this causing problems before.
kglad
Community Expert
Community Expert
May 5, 2008
you have, at least, one malformed php line:

$toSave ="Name=$name&Comments=$comments;

should be:

$toSave = "Name=" .$name1. "&Comments=" .$comments;
Inspiring
May 4, 2008
Does it trace...

function done(event:Event) {
----------------------------------------->
trace(event.target.data.writing);
tester.text = event.target.data.writing;
}

what does the data.writing do?




"jishi9" <webforumsuser@macromedia.com> wrote in message
news:fvl0qm$a9c$1@forums.macromedia.com...
>I am trying to send data (variables) to a php file. I keep getting an error
> even though I am using the exact method used in Flash Help (something like
> Using External Data).
>
> //////Actionscript 3 (tester is a dynamic textfield on stage)
>
> var req:URLRequest = new URLRequest();
> req.url = "save.php";
> req.method = URLRequestMethod.POST;
> var vars:URLVariables = new URLVariables();
> vars.Name = "some name";
> vars.Comments = "some comment";
> req.data = vars;
>
> var loader:URLLoader = new URLLoader();
> loader.dataFormat = URLLoaderDataFormat.VARIABLES;
> loader.load(req);
> loader.addEventListener(Event.COMPLETE, done);
>
> function done(event:Event) {
> tester.text = event.target.data.writing;
> }
>
>
>
>
> 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$iinit()
> at flash.net::URLLoader/flash.net:URLLoader::onComplete()
>
>
>
> ////////PHP Code (saves data to a text file)
>
> <?php
> //Capture data from $_POST array
> $name = $_POST['Name'];
> $comments = $_POST['Comments'];
> //Make one big string in a format Flash understand
> $toSave ="Name=$name&Comments=$comments;
> //Open a file in write mode
> $fp = fopen("comments.txt", "w");
> if(fwrite($fp, $toSave)) echo "writing=Ok&";
> else echo "writing=Error&"
> fclose($fp);
> ?>
>

jishi9Author
Inspiring
May 6, 2008
It never makes it to the done function. I get the error before after loading the request.

As for the writing variable, it is just a confirmation that the data has been saved.


reply to kglad : as for the php mistake i have corrected it but I still have the same issue.