Skip navigation
Currently Being Moderated

undefined resutl tryng to get data to flash with php

Mar 8, 2012 6:32 PM

Hello there,

I'm not   coder but I understand well the way it work and can manipulate script already written. I cross with a problem that I do not understand why is happening. I a AS3 and php getting the results from the database. The php is working fine since I test it. but the request from the flash give me a result of "undefined" .I have a text area on the flash file in which is supposed to show the results from the php. This code so far:

AS3:


import flash.display.*;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;


    
// Assign a variable name for our URLVariables object
var variables:URLVariables = new URLVariables();
//  Build the varSend variable
var request:URLRequest = new URLRequest();
request.url = "ckBxSample_disp.php";
request.method = URLRequestMethod.POST;
request.data = variables;
// Build the varLoader variable
var varLoader:URLLoader = new URLLoader();
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
try
{
    
    varLoader.load(request);
}
catch (error:Error)
{
    trace("Unable to load URL");
}
// Handler for PHP script completion and return
      function completeHandler(event:Event):void{
       // Load the response from the PHP file
      if (event.target.data.returnBody == "") {
        output_txt.text = "No data coming through";
      } else {
        output_txt.condenseWhite = true;
        output_txt.htmlText = "" + event.target.data.returnBody;
        
      }
    
    }

// Send the data to the php file
varLoader.load(request);
// Ready the variables for sending
variables.requester = "phpRows";

in the php :

 



if ($_POST['requester'] == "phpRows") {

sql connection code

 

then mysql query here

$sql = mysql_query("SELECT blah bla bla

//then the while loop for all records I want to pull

while($row = blah ){

//calling the rows

$id = $row["id"];

//sending th results to be calle by flash

$body = " $id ";

}

//flash results

print "returnBody = $body";

//close con

    mysql_close();

    exit();

}

This is most the code and I get a undefined result in the textarea in flash

can anyone help? please

thanks in advance

 
Replies 1 2 Previous Next
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 8, 2012 10:16 PM   in reply to pao1011

    use:

     

     

    
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequestMethod;
    
    
        
    // Assign a variable name for our URLVariables object
    var variables:URLVariables = new URLVariables();
    //  Build the varSend variable
    var request:URLRequest = new URLRequest();
    request.url = "ckBxSample_disp.php";
    request.method = URLRequestMethod.POST;
    
    // Build the varLoader variable
    var varLoader:URLLoader = new URLLoader();
    varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    varLoader.addEventListener(Event.COMPLETE, completeHandler);
    try
    {
        
        varLoader.load(request);
    }
    catch (error:Error)
    {
        trace("Unable to load URL");
    }
    // Handler for PHP script completion and return
          function completeHandler(event:Event):void{
           // Load the response from the PHP file
          if (event.target.data.returnBody == "") {
            output_txt.text = "No data coming through";
          } else {
            output_txt.condenseWhite = true;
            output_txt.htmlText = "" + event.target.data.returnBody;
            
          }
        
        }
    
    // Send the data to the php file
    
    // Ready the variables for sending
    variables.requester = "phpRows";
    request.data = variables;
    varLoader.load(request);

    in the php :

     



    if ($_POST['requester'] == "phpRows") {

    sql connection code

     

    then mysql query here

    $sql = mysql_query("SELECT blah bla bla

    //then the while loop for all records I want to pull

    while($row == "blah" ){

    //calling the rows

    $id = $row["id"];

    //sending th results to be calle by flash

    $body = " $id ";

    }

    //flash results

    print "returnBody = $body";

    //close con

        mysql_close();

        exit();

    }

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 9, 2012 6:35 AM   in reply to pao1011

    yes, it matters.

     

    copy the changed code you're using and paste it here.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2012 7:12 AM   in reply to pao1011

    You are returning WAY too much information, a whole HTML page. You can't do that. You need to strip the response so the only thing that's outputted from http://digicinelatino.com/flash/ckBxSample_disp.php is literally this:

     

    returnBody=George+Juarez

     

    The vars need to be in URL encoded format.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 9, 2012 8:17 AM   in reply to pao1011

    you're not using the code i suggested.  aqain, order is important:

     

    variables.requester = "phpRows";

    request.data = variables;

    varLoader.load(request);

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2012 8:36 AM   in reply to pao1011

    Let me rephrase. You're looking at the page http://digicinelatino.com/flash/ckBxSample_disp.php in a browser. Flash is not looking at that page like a browser, it is looking at it like raw text. In other words, view source on that page and look at what flash sees. It does not just see:

     

    returnBody = Some Dude

     

    It sees:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">...........etc

     

    What you want to send as a response back to flash is a GET encoded response. Nothing else and certainly not in a HTML page.

     

    Make this file: result.txt and ONLY add in this:

    returnBody=George+Juarez

     

    Then change the line:

    request.url = "ckBxSample_disp.php";

     

    to:

    request.url = "result.txt";

     

    See what you get back in returnBody.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2012 10:16 AM   in reply to pao1011

    If you need to send a lot and/or complex amount of data then consider JSON. There are plenty of links at the bottom of the page for JSON encode/decoders that make it really easy to send data from one place to another. LoadVars-esque techniques never really suited me. Anyhow of course you can use the PHP, but you need to edit the PHP page itself so it stops spitting out all that HTML around it. What you should end up should look exactly like the text file above.

     

    Again, I'm not suggesting use a text file. I just wanted to show you it is working properly but you're not formatting your response to flash properly. The text file shows you what you need to do. Now modify the PHP so it strips out all the HTML crap and just format the response properly.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2012 11:14 AM   in reply to pao1011

    You need to format your response like a GET encoded URL. You are putting new lines (\n) in your response and that will screw it up.

     

    // valid response:

    userAName=Joe&userAID=31&userBName=Bob&userBID=32&userCName=Jack&userCID=33

     

    // INVALID response, makes no sense to flash

    Joe 31

    Bob 32

    Jack 33

     

    You also need to strip out the HTML from your response. You still have HTML crap in there. Nothing but the data should be printed. No <!DOCTYPE html PUBLIC "-//W3...... etc. Strip all that crap out. View your pages source that returns results to verify you removed it.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 9, 2012 1:36 PM   in reply to pao1011

    Sure, but in that case you don't want to use the URLLoaderDataFormat.VARIABLES if you just want to print a formatted string into a htmlText. What you're trying to do is properly format it so flash can generate variables from the text you're reading. And yes, PHP needs to be executed so it needs a server.

     

    If you just want to dump a string in a htmlText then use URLLoaderDataFormat.TEXT. Then dump the response into some dynamic text field. There's no need to convert it to variables unless you need to break the data up in a controlled way into separate variables.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 9, 2012 4:15 PM   in reply to pao1011

    copy and paste the following and upload your files to a server.  post a link to your embedding html.

     


    import flash.display.*;
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequestMethod;


       
    // Assign a variable name for our URLVariables object
    var variables:URLVariables = new URLVariables();
    //  Build the varSend variable
    var request:URLRequest = new URLRequest();
    request.url = "ckBxSample_disp.php";
    request.method = URLRequestMethod.POST;

    // Build the varLoader variable
    var varLoader:URLLoader = new URLLoader();
    //varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    varLoader.addEventListener(Event.COMPLETE, completeHandler);
    try
    {
       
        varLoader.load(request);
    }
    catch (error:Error)
    {
        trace("Unable to load URL");
    }
    // Handler for PHP script completion and return
          function completeHandler(event:Event):void{
           // Load the response from the PHP file
          if (event.target.data.returnBody == "") {
            output_txt.text = "No data coming through";
          } else {
            output_txt.condenseWhite = true;
            output_txt.htmlText = "" + event.target.data;
           
          }
       
        }

    // Send the data to the php file

    // Ready the variables for sending
    variables.requester = "phpRows";

    request.data = variables;

     

    varLoader.load(request);
     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 9, 2012 6:15 PM   in reply to pao1011

    again, upload your files (html, php and swf) to a server.  post a link to your embedding html.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 9, 2012 10:07 PM   in reply to pao1011

    now, fix your php and retest.

     

    <php

        //I remove the if condition to show the result from the database

        //request from Flash

                   ($_POST['requester'] == "phpRows") ;

     

    //Connect to the databese

    include_once "connect_to_mysql.php"

     

    $tbl_name = "table_name";

    $body = "";

    $con = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)

    or die("Opps Fail to connect to the host");

    mysql_select_db($mysql_database, $con) or die("Opps fail to connect to database");

     

     

     

    $sql = mysql_query("SELECT * FROM $tbl_name");

    $result=mysql_query($sql);

    while($row = mysql_fetch_array($sql)){

     

     

     

    $id = $row["id"];

    $uname = $row["uname"];

    $client_song = $row["client_song"];

     

    $body = "$id&$uname&$client_song";

     

        }

    print $body;

     

        mysql_close();

        exit();

        ?>

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2012 8:41 AM   in reply to pao1011

    You do not need to stick with the VARIABLES route.

     

    However, if you do, you are STILL printing out an empty line before you print your variables. You also are not providing a variable name for all the variables being returned. You also are not urlencoding your results.

     

    Here is what your script is printing out:

    "

    returnBody= 37&George Juarez&Los del Rio - La Macarena"

     

    I added the quotes myself for representation. Notice the quote starts, then an empty line, then on the next line your data begins? You can't do that. Get rid of the empty first line. You have to think from a code perspective, not human. Your code doesn't know your first line, which is empty, isn't what you want. That's why you get no response in variable form.

     

    Second, you have to assign a variable name to every piece of data. That means you want to assign a variable name to the data '37', another to 'George Juares', another to 'Los del Rio - La Macarena'. You also should not have anything invalid in an URL, such as a space. A space is represented by %20.

     

    What you should be trying to do is something like this:

    id=37&name=George%20Juarez&location=Los%20del%20Rio%20-%20La%20Macarena

     

    Then you will have assigned each piece of data with a variable name and will know how to access it separately.

     

     

    Note: You only want to url encode the values, not the variable names. You also want to keep the variable names to standard variable naming conventions. AS3 has a function to properly encode a string for you, called escape().

     

    For example:

     

    var value:String = "This is a string";

    trace(value); // This is a string

    value = escape(value);

    trace(value); // This%20is%20a%20string

     

    I still say you get away from the VARIABLE usage of URLLoader so you can keep the formatted version of the output you so desire. If you're going to use VARIABLES, then learn how to form the proper response. ALL of the information you need is right here.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2012 1:01 PM   in reply to pao1011

    Lets be really clear here. I'm going to actually type the same thing 3 times in a row so you understand it.

     

    returnBody is the name of a variable.

    returnBody is the name of a variable.

    returnBody... is... the... name... of... a.... variable.

     

    Stop fixating on "returnBody". Please.

     

    The convention I'm trying to help you understand is this:

     

    var=value

     

    That is it. If you need more than 1 value, then you separate them with an ampersand (&).

     

    e.g.:

    var1=value1&var2=value2

     

    That means you sent back 2 variables. If you traced the value of var1 it would be 'value1'. If you traced var2 it would be 'value2'.

     

    This is a concept that has elluded you this entire thread. Stop trying to send back the whole result wrapped inside this 'returnBody' variable. Learn that I'm trying to show you how to return the values and assign them to separate values so you can use them how you wish.

     

    If you want to just print the output of the PHP files results, then use the code that has been shown above. Do not use VARIABLES, use the TEXT version of URLLoader and send the data returned directly to a dynamic text field using its htmlText property.

     

    Otherwise hire someone that understands actionscript and the web.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 10, 2012 1:19 PM   in reply to pao1011

    upload Scripts/swfobject_modified.js

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 10, 2012 1:25 PM   in reply to pao1011
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2012 1:42 PM   in reply to pao1011

    Nobody jumps in here just to give you grief. When you understand how to format a "GET" formatted URL string as well as understand URL encoding you will read this thread again and realize the answer has been supplied to you multiple times.

     

    My apologies if you think I'm insulting you but if you are told 1+1=9999 and we tell you 1+1=2 and you continue not to listen, you may be called on it.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 10, 2012 1:43 PM   in reply to pao1011

    you still have

     

    print returnBody=$body;

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 10, 2012 2:39 PM   in reply to pao1011

    humor me and upload your swf's embedding html, change your php to print $body and post a link to your embedding html.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 10, 2012 2:55 PM   in reply to pao1011

    1.  open your swf's fla in Flash Pro

    2.  click file/publish settings and make sure html and swf are ticked.

    3.  click file/publish.  don't use dreamweaver and don't edit the html file.

    4.  upload the published html and swf to some directory on your http://digicinelatino.com server.

    5.  post a link to your uploaded html file.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 10, 2012 2:52 PM   in reply to pao1011

    Hi,

     

    to be honest ... there is a problem with the tutorial you are using

     

    Its overall php structure is

    setup mysql

    while there are records {

        form a result

    }

    send result

     

    So if there is more than one result in the db, only the last one would be sent.

     
    |
    Mark as:
  • kglad
    72,228 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 10, 2012 4:44 PM   in reply to pao1011

    you're still trying to access a returnBody property from that data string and you're still using the same problematic php code.

     

    "Flash is asking for return results from returnBody"

    returnBody=38&Paola F. Gonzalez&Shakira - Ciega, Sordomuda

     
    |
    Mark as:
1 2 Previous Next

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points