• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

undefined resutl tryng to get data to flash with php

Explorer ,
Mar 08, 2012 Mar 08, 2012

Copy link to clipboard

Copied

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

TOPICS
ActionScript

Views

6.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 10, 2012 Mar 10, 2012

your php is mis-formatted.  use:

<?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 *

...

Votes

Translate

Translate
Community Expert ,
Mar 08, 2012 Mar 08, 2012

Copy link to clipboard

Copied

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();

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 08, 2012 Mar 08, 2012

Copy link to clipboard

Copied

I try but still displaying "undefined" on the textarea named "ouput_txt". Do the  "request.data = variables" matter the position on where is located? thanks for the reply.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

yes, it matters.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

I did changed it but didn't work, still showing undefined on the textarea. Sample here: http://digicinelatino.com/flash/phpFlashSample.php

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

I do not undestand  the "too much information part". The result should on the $body and  display three rows from the database with html formated code which flash is caplable to do so.Iif you see the results on the chkbxSample_disp.php it return the three rows with variuos results of the whole data stored in the database. To my understanding flash is caplable to show a hundred of records from the database. and the return you say "returnBody=George+Juarez" you posting the results without calling them from the database since should display the id, uname and song_ client to pull the records from the database. To my undestanding so far is that flash is not sending the - ($_POST['requester'] == "phpRows" - or flash not reading the - print "returnBody = $body";- and that is why return undefined. But I do not know what.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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

variables.requester = "phpRows";

request.data = variables;

varLoader.load(request);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

To sinious - OK I understand your point with the text file, I can do that, then the php is useless because how do I call the records from the database. Text file most be updated manually and the purpose of the php file is to load the record from the database. so how I can do that? I have a file witha datagridcomponent  that read an php xml pharser. so then I need to change the code complete to read xml and my knowledge is not that extense. BTW thanks for the reply.

To kglad.com - yes I did the way you suggested and keep getting the undefined. thaks for the reply too

Maybe I make the php to write the results in a text fille but how I trigger it from flash,  since flash will read the text file?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

Nahh. I think flash should read the php since I can send a php form and all the records from flash are inserted on the database also i have a datagrid file that read the xml data from a php file. How is that now will not read the html code since the call "output_txt.htmlText = "" + event.target.data.returnBody" is html text? something is missing in the code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

I change the php code line like this to my understanding :

$body = "$id \n $uname \n $client_song";

print "returnBody = $body";

the php return the results plain but the flash file continue undefined

I setup a try{} statement in flash as you can see in the flash code and relocate under the "request.method = URLRequesMethod.POST" and now trow an the error trace unable to load url.  I guess that is what happening that the flash is not reading the php. I get rid of - request.url = "chkbxSample_disp.php"; - and replace with - new URLRequest("chkbxSample_disp.php") - and still given me the error. I even setup a - var returnBodyString; - nothing happens ..... ?????

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

I did what you told me, I strip the html leave pure php and put the "&" and the result on the texaea result is blank tested on the server. My flash output trow this error:

Unable to load URL

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()

    at flash.net::URLLoader/onComplete()

but I think is because I test it locally and there's no php result.

What I don't undestand is since flash can read html with the htmlText why the new lines can't be read with  \n or <br /> ? is not that the way to format html text to display in flash? as far as I know you can pull tags like <font> <p> <b> <u><br /> and some others even you can load images with a txt file with the <img src""> tag I did that in one of my files also links links with the <a herf=""> tag That's my question but still the sample not works anyways.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

Since I'm loading data from a script I think I have to stick with the URLLoaderDataFormat.VARIABLES. The problem is that  my code come up with an empty result so far. Why? I don't have any idea!.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

I paste the code in flash then publish and  load the swf file to the server. No luck. This is the link http://digicinelatino.com/flash/phpFlashSample.php you will find both of the codes  I'm using below the swf embeded file.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

I'm already did that the files that are on the server are with the code you provide me the likn I already give it in the prior post. http://www.digicinelatino.com/flash/phpFlashSample.php this is the link again. The textarea is blank, no data!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2012 Mar 09, 2012

Copy link to clipboard

Copied

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();

    ?>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 10, 2012 Mar 10, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 10, 2012 Mar 10, 2012

Copy link to clipboard

Copied

To KGad.com : I did Change the code still given blank as a result. If I print the $body by it self then how flash read the returnBody request?

To sinious: I understand what you are explaining me. The problem is that in order to pull a record from the database without the spaces then I need to write exactly as flash will like to read the result then when I want to display the records normally they will display in "flash required laguage" Therefore, I need to make the databade record to strings with "" quotes something like this?

print "returnBody= id:\"&$id&\"uname:&\"$uname\"&client_song:&\"$client_song\"";

then the display in php will be:

returnBody= id:"&37&"uname:&"George Juarez"&client_song:&"Los del Rio - La Macarena"

I assume that is what you try to explain to me so far. Still, the result is the same, textarea blank.

Beside, I try to this too and return blank print "returnBody=Pedro"; there are no spaces or anythig that flash could not read, why still not given a result?

Let me explain better my knowledge of flash AS is not so good, Mostly, I follow tutorials on the net for what I need to do. Most of the things I do, take me time to make  suit my needs but at the end,  I accomplished what I want to do  and in the way, I learn something new. What you teaching me here, for me, is very valuable because I understand many thing that I have doubts or dont knew before. URLVariables, URLRequest, URLLoader, are complex script to do, well at least for me, because the resources tutorials are very few on the net, specially when you work with external data like PHP, I hope tou understand  and I apologize if I look like with no knowledge of the subject. Thank you both of you for your responses.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 10, 2012 Mar 10, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines