Skip to main content
Known Participant
July 15, 2009
Answered

URLVariables.decode() error when converting to variables

  • July 15, 2009
  • 2 replies
  • 3350 views

Hello!

I am passing a simple string from a PHP file, that looks like this

PHP Code:

$connect = mysql_connect("localhost", "root", "");
mysql_select_db("dev", $connect);
$result = mysql_query("SELECT * FROM users");
$cant = 0;
while($row=mysql_fetch_array($result)){
    echo "Name$cant=$row[name]&Email$cant=$row[email]&";
    $cant++;
}

PHP output:

Name0=jane doe&Email0=jane@huj.com&Name1=jane doe&Email1=jane@huj.com&Name2=jane doe&Email2=jane@huj.com&Name3=jane doe&Email3=jane@huj.com&Name4=jane doe&Email4=jane@huj.com&

I am asking flash to prase this data and display each field in a text field, it seems as it is failing at the  myTextLoader.dataFormat = URLLoaderDataFormat.VARIABLES; because when i take it out or switch the dataFormat to TEXT, it does not produce that error

Flash Code:

var myTextLoader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://localhost/flash.php");
myTextLoader.load(request);
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

function onLoaded(e:Event):void {
for (var i:Number = 0; i < 9; i++) {
//array for text fields
var nameField:TextField = new TextField();
var emailField:TextField = new TextField();
nameField.text = myTextLoader.data["Name" + i];
emailField.text = myTextLoader.data["Email" + i];
emailField.y += 50;
emailField.x = 200;
nameField.y += 50;
nameField.x = 150;
addChild(nameField);
addChild(emailField);
}
}


Flash Error:

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

Any Suggestion as to what needs to be changed in order for it to work, i tried to trace stuff, change the variable scope etc.

Read up on this online at Flash Docs, but it seems as though the syntax is correct

Thank you

This topic has been closed for replies.
Correct answer kglad

you can use:


var myTextLoader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://localhost/flash.php");
myTextLoader.load(request);
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

function onLoaded(e:Event):void {

var s:String = String(myTextLoader.data);
var num:uint = s.split("&").length/2;

var tfY = 0;
for (var i:Number = 0; i < num; i++) {
//array for text fields
var nameField:TextField = new TextField();
var emailField:TextField = new TextField();
nameField.text = myTextLoader.data["Name" + i];
emailField.text = myTextLoader.data["Email" + i];
emailField.y = tfY;
emailField.x = 200;
nameField.y = tfY;
nameField.x = 150;

tfY += 50;
addChild(nameField);
addChild(emailField);
}
}


2 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 16, 2009

you can use:


var myTextLoader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://localhost/flash.php");
myTextLoader.load(request);
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
myTextLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

function onLoaded(e:Event):void {

var s:String = String(myTextLoader.data);
var num:uint = s.split("&").length/2;

var tfY = 0;
for (var i:Number = 0; i < num; i++) {
//array for text fields
var nameField:TextField = new TextField();
var emailField:TextField = new TextField();
nameField.text = myTextLoader.data["Name" + i];
emailField.text = myTextLoader.data["Email" + i];
emailField.y = tfY;
emailField.x = 200;
nameField.y = tfY;
nameField.x = 150;

tfY += 50;
addChild(nameField);
addChild(emailField);
}
}


georgeebAuthor
Known Participant
July 16, 2009

You are a genius, i appreciate your hel, but it seems as though using the code above and my php script, i am still not cathing this stuff right.


This is the error i get

TypeError: Error #2007: Parameter text must be non-null.
    at flash.text::TextField/set text()
    at sample_fla::MainTimeline/onLoaded()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

The problem with this is that i does not specify where exactly the problem is, it simply says that a text param can not be null, it does not poin to which line it has a problem with.

georgeebAuthor
Known Participant
July 16, 2009

Thank you so much

It is working now, i added a value at the end of the php string and it is fixed

Thanks

kglad
Community Expert
Community Expert
July 15, 2009

do not output a terminal ampersand.

(and your textfields will only display the last name/email and fix your for-loop to match the number of variables being returned.)

georgeebAuthor
Known Participant
July 15, 2009

ok

i tried to remove the last one


PHP code:

echo "Name$cant=$row[name]&Email$cant=$row[email]&(i removed this one)";

so the new code looked like so:


echo "Name$cant=$row[name]&Email$cant=$row[email]";

Still getting the same error

Any thoughts

kglad
Community Expert
Community Expert
July 15, 2009

show your php output and did you correct the other two errors?