A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: [v7] Saving data to file (again)

  1. #1
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    [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_ERRORl_ioerror);
        
    l.addEventListener(SecurityErrorEvent.SECURITY_ERRORsecerror);
        
    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.

  2. #2
    Senior Member
    Join Date
    Dec 2002
    Location
    Netherlands
    Posts
    1,632
    As far as I know it's $_POST instead of _$POST .

  3. #3
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    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.

  4. #4
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    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;
    ?>

  5. #5
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    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..

  6. #6
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    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{
        
    $writefilefopen($file"w");

        
    fwrite($writefile$contents);
        
    fclose($writefile);
        
       
    $readfilefopen($file"r");
       
    $contents2 fread($readfilefilesize($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{

        
    = new URLRequest("brent.php");
        
    s.method=URLRequestMethod.POST;
        
    = 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_ERRORl_ioerror);
        
    l.addEventListener(HTTPStatusEvent.HTTP_STATUShandleHttpStatus);
        
    l.addEventListener(SecurityErrorEvent.SECURITY_ERRORsecerror);
       
        
    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_ERRORl_ioerror);
              
    l.removeEventListener(HTTPStatusEvent.HTTP_STATUShandleHttpStatus);
              
    l.removeEventListener(SecurityErrorEvent.SECURITY_ERRORsecerror);
              
    URLLoader(event.target); 
               
    txt2.htmlText=l.data.returnMe
                
              } 
    Running live here:

    http://www.km-codex.com/examples/Bre..._Response.html

  7. #7
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    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.

  8. #8
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    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

  9. #9

  10. #10
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    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.

  11. #11
    Senior Member
    Join Date
    Apr 2006
    Location
    Yugoslavia
    Posts
    147
    Can I please have source files? I tried to recreate it, but was obviously doing something wrong.

    Thank you!

  12. #12
    That web bloke Stoke Laurie's Avatar
    Join Date
    Jan 2006
    Location
    England
    Posts
    869
    I think Brett will be posting something soon. - work permitting.

  13. #13
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    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.

  14. #14

  15. #15
    Senior Member
    Join Date
    Apr 2006
    Location
    Yugoslavia
    Posts
    147
    Danke schön!!!!!!!!!!!!!!

  16. #16

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center