-
KoolMoves Moderator
[v7] Saving data to file (again)
OK maybe I'm just tired but this is wearing me out...
have richtextbox data I want to send to PHP to save.
Here's what I'm trying at the moment.
PHP Code:
//AS3 code
pb1.onClick=function(){
s=new URLRequest("savefile.php");
s.method=URLRequestMethod.POST;
v=new URLVariables();
v.filename=sfile;
v.contents=rt1.htmlText;
s.data=v;
l=new URLLoader();
l.dataFormat=URLLoaderDataFormat.VARIABLES;
l.addEventListener(Event.COMPLETE,filesent);
l.addEventListener(IOErrorEvent.IO_ERROR, l_ioerror);
l.addEventListener(SecurityErrorEvent.SECURITY_ERROR, secerror);
l.addEventListener(Event.PROGRESS,update);
l.load(s);
}
function update(e:Event){
}
function l_ioerror(e:IOErrorEvent){
txt1.text="IO Error";
}
function secerror(e:SecurityError){
txt1.text="Security Error";
}
function filesent(e:Event){
txt2.text=l.data;
}
and the PHP code is as follows (I think it's ok)
PHP Code:
<?php
$file=_$POST['filename'];
$contents=_$POST['contents'];
$fh=fopen($file,w);
$result=fputs($fh,$contents);
echo $result;
?>
But It's not working at this point.
I plan on adding a check to get $result back from the PHP as well to verify.
-
As far as I know it's $_POST instead of _$POST .
-
KoolMoves Moderator
LOL that's funny, See I was tired... Let me try fixing that... I was so focused on my AS3 I didn't even look close at the PHP.
-
up to my .as in code
You'll get in the habit of direct hitting a back end with fake POST or GET variables to separate what is a front end issue (As scripting) from back end issue (possible PHP or sql or write permission or etc problem) to help narrow down what is failing.
<?php
//$file=_$POST['filename'];
//$contents=_$POST['contents'];
$file='faketestfile.txt";
$contents='blahblahblahblah';
$fh=fopen($file,w);
$result=fputs($fh,$contents);
echo $result;
?>
Last edited by Chris_Seahorn; 02-18-2009 at 10:25 PM.
-
KoolMoves Moderator
Yeah I've done that. I also have a tool in Firefox that allows me to send false Headers to test with.
Looks like it's also something in the AS3 that's not working..
-
up to my .as in code
There is no way it CAN work. Too many errors
_$POST['filename'];
should be
$_POST['filename']; (etc etc)
You've told your URLLoader to expect name/value pairs:
l.dataFormat=URLLoaderDataFormat.VARIABLES;
Yet send in no pair via the php response:
$result=fputs($fh,$contents);
echo $result;
I'm assuming you want to read the newly created file contents and send them in to confirm they were written and this is not how it is done. What you are sending in is something completely different.
You want to POST...write...close....reopen....read.... assign read contents to variable and send in as name/value pair like so:
PHP:
PHP Code:
<?php $file=$_POST['filename']; $contents=$_POST['contents']; if(!$file || !$contents){ print "returnMe=Server Error!"; exit; }else{ $writefile= fopen($file, "w");
fwrite($writefile, $contents); fclose($writefile); $readfile= fopen($file, "r"); $contents2 = fread($readfile, filesize($file)); fclose($readfile); print "returnMe=$contents2"; } ?>
Fun file AS3:
PHP Code:
import flash.errors.*; import flash.events.*; import flash.geom.*; import flash.net.*;
var s:URLRequest; var v:URLVariables; var f:String="brent.txt";
pb1.addEventListener(MouseEvent.CLICK,sendOut);
function sendOut(e:MouseEvent):void{
s = new URLRequest("brent.php"); s.method=URLRequestMethod.POST; v = new URLVariables(); v.filename = f; v.contents = rt1.htmlText; s.data = v; l=new URLLoader(); l.dataFormat=URLLoaderDataFormat.VARIABLES; l.addEventListener(Event.COMPLETE,filesent); l.addEventListener(IOErrorEvent.IO_ERROR, l_ioerror); l.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleHttpStatus); l.addEventListener(SecurityErrorEvent.SECURITY_ERROR, secerror); l.load(s); }
function handleHttpStatus(event:HTTPStatusEvent):void{ //something here }
function update(e:Event){ } function l_ioerror(e:IOErrorEvent){ txt1.text="IO Error"; } function secerror(e:SecurityError){ txt1.text="Security Error"; }
function filesent(event:Event):void{ l.removeEventListener(Event.COMPLETE,filesent); l.removeEventListener(IOErrorEvent.IO_ERROR, l_ioerror); l.removeEventListener(HTTPStatusEvent.HTTP_STATUS, handleHttpStatus); l.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, secerror); l = URLLoader(event.target); txt2.htmlText=l.data.returnMe; }
Running live here:
http://www.km-codex.com/examples/Bre..._Response.html
Last edited by Chris_Seahorn; 02-19-2009 at 11:24 PM.
-
up to my .as in code
As a side note...let me help users who may or may not be reading AS3 books to help get going with dynamic data and server side AS3. IMO the AS3 Cookbook IS the definitive book covering the subject matter available. The sheer amount of code snippets is worth the purchase. Other books like the Bible and Essential AS3 cover it only slightly and focus more on OOP and class and package structure and core concepts.
That said...the Cookbook is also the one book with the most syntactical errors (the errata page at their website is a testament to just how many errors there were at print time). Whoever proof read that book before sending it to the printers (especially since it is a book with so much scripting) should be shot
Best book I ever bought and worst book I ever bought at the same time (it was the first book I bought two years ago when picking up Flex to learn AS3). Sometimes you gotta take the good...with the bad I guess.
-
KoolMoves Moderator
Thanks Chris, I had it working partly, The PHP was me working too tired to think.
I was stuggling with getting the server response and from your example I now have better idea of what I was doing wrong. Thanks
-
up to my .as in code
Been a while since I wrote a file to server....was a nice refresher
-
KoolMoves Moderator
I've been so busy with work I've been away from PHP and Koolmoves for some time. Thought I had a slow week, so promised to get some stuff done for a few lingering clients and of course got busy again.... LOL
Been working on this and some MySql stuff in PHP for an old project that needed updating. All after I get home from working 12+ hours on a shoot.
-
Can I please have source files? I tried to recreate it, but was obviously doing something wrong.
Thank you!
-
That web bloke
I think Brett will be posting something soon. - work permitting.
Last edited by Stoke Laurie; 02-20-2009 at 07:31 AM.
-
up to my .as in code
We'll add it to the codex as post with source code Joca. We have tons of dynamic Koolmoves code planned for 2009 and beyond and It's one of the reasons we started the website
Be forewarned...nearly all code coming from us will require KM7 from here out.
Last edited by Chris_Seahorn; 02-20-2009 at 11:18 AM.
-
up to my .as in code
It's posted for download joca
-
Danke schön!!!!!!!!!!!!!!
-
up to my .as in code
You know where to find me if you need me....have fun
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|