A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: URLLoader issue - AS3 to PHP and back again

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    15

    URLLoader issue - AS3 to PHP and back again

    Hi,

    I'm just testing out sending variables between AS3 and PHP.

    The code below sends variables to PHP fine.

    PHP returns a string "returnedfirstname=bob" just fine. I've tested that by using: trace(loader.data).

    The problem is, I can't seem to create a variable in AS3 called returnedfirstname and populate it with "bob" from the loader.data:

    Code:
    var request:URLRequest = new URLRequest ("<someurl>");
    request.method = URLRequestMethod.POST;
    				
    var variables:URLVariables = new URLVariables();
    				
    variables.firstName = "Bob";
    request.data = variables;
    				
    var loader:URLLoader = new URLLoader (request);
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.load(request);
    
    function onComplete (event:Event):void {
    	var returnedfirstname = loader.data.returnedfirstname;
    	 trace (returnedfirstname);
    };
    trace(returnedfirstname) gives me nothing.

  2. #2
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Try out this and see if it works:

    PHP Code:
    function onComplete (event:Event):void {
        var 
    returnedfirstname:String event.target.data.returnedfirstname;
         
    trace (returnedfirstname);
    }; 
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    15

    Still no good

    Thanks florianvanthuyn but I get the same results.

    to confirm, I put in:

    Code:
    switch(loader.dataFormat) {
    	case URLLoaderDataFormat.TEXT :
    		trace("completeHandler (text): " + loader.data);
    		break;
    	case URLLoaderDataFormat.BINARY :
    		trace("completeHandler (binary): " + loader.data);
    		break;
    	case URLLoaderDataFormat.VARIABLES :
    		trace("completeHandler (variables): " + loader.data);
    		break;
    }
    and get the result:

    completeHandler (variables): returnedfirstname=bob

    So it's definitely getting, as variables, the right data back into Flash but:

    returnedfirstname = event.target.data. returnedfirstname

    return null

    as does

    returnedfirstname = variables. returnedfirstname

  4. #4
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    I'll just post how I managed to get it work one time with the code I gave, so maybe it's the way you send it back with your php?

    PHP Code:
    <?php
    // receive variables
    $input_name $_POST['user'];
    $input_pw $_POST['password'];

    // checking if valid
    if($input_name == "admin" && $input_pw == "password")
    {
        print 
    "login=accepted";
    }
    else
    {
        print 
    "login=denied";
    }
    ?>
    So far the php, now tha as3:

    PHP Code:
    var myURL:String "scrapbook/config/login.php"

    var 
    myVars:URLVariables = new URLVariables(); 
    myVars.user login.user.text
    myVars.password login.pw.text
    myVars.sendRequest "parse";    
                
    var 
    myUrlReq:URLRequest = new URLRequest(myURL); 
    myUrlReq.data myVars
    myUrlReq.method URLRequestMethod.POST
                
    var 
    myUrlLoader:URLLoader = new URLLoader(); 
    myUrlLoader.dataFormat URLLoaderDataFormat.VARIABLES
    myUrlLoader.addEventListener(Event.COMPLETEsuccessHandler); 
                
    myUrlLoader.load(myUrlReq);

    private function 
    successHandler(e:Event):void

        if(
    e.target.data.login == "accepted"
        { 
            
    // correct stuff
        

        else 
        {
            
    // incorrect stuff
        


    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

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