Getting php variables into dynamic text boxes

<?php
    include ('connect2db.php');

$result = mysql_query("SELECT * FROM userdata WHERE username = '$username' ");
      $details = mysql_fetch_row($result);

      $license =  $details[0];
      $wins =  $details[1];
      $losses =  $details[2];
      $team =  $details[3];
      $balance = $details[4];
   
      print "$license";
      print "$wins";
      print "$losses";
      print "$team";
      print "$balance";
?>


Using this php code, how to I put those variables into dynamic text boxes
mrhellAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CalronCommented:
First you will have to make a little change to your php file:

 <?php
    include ('connect2db.php');

$result = mysql_query("SELECT * FROM userdata WHERE username = '$username' ");
     $details = mysql_fetch_row($result);

     $license =  $details[0];
     $wins =  $details[1];
     $losses =  $details[2];
     $team =  $details[3];
     $balance = $details[4];
   
     $text = "&license=" . $license . "&wins=" . $wins . "&losses=" . $losses . "&team=" . $team . "&balbance=" . $balance;

 $text = utf8_encode($text);   // this is only really necessary if you have unicode characters or special characters

    echo $text;
     
?>

Then in flash you will have to have something like the following:

var loader1 = new LoadVars();
loader1.onLoad = function() {
  // in here we now should have 5 variables, license, wins, losses, team, balance   so set them to a textfield
  mytextfield.text = license;
  etc.
}
loader1.load("myphpscript.php");
0
mrhellAuthor Commented:
I put that in and in all my boxes and "undefiened" prints out in them even though I have data in the database...
0
rascalpantsCommented:
have you looked at the output page yet?

I think you need to make sure that your PHP is outputting to the web browser properly...

post the url to that page...


rp
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

CalronCommented:
Yes the first thing I would try is what rascalpants says. Test that the php script is working correctly. You can do that by just calling the url in a browser and you should see something like this:

&license=1&wins=1&losses=1&team=1&balbance=1

instead of =1 everywhere you should be seeing the values from your db.

If that doesn't work, you will have to check your php script to make sure that it works correctly
0
mrhellAuthor Commented:
When I put in www.mywebsite.com/userdata.php it comes up with my defualt database details. When I put in www.mywebsite.com/userdata.php?username=myusername it will come up with all the details for my username. So the php code appears to be working fine.
0
CalronCommented:
So the next step is to make sure that flash is loading your php output correctly. Change the following:

add a new textfield and give it an instancename i.e. test

var loader1 = new LoadVars();
loader1.onLoad = function() {
  test.text = "Yes this method was called";
  // in here we now should have 5 variables, license, wins, losses, team, balance   so set them to a textfield
  mytextfield.text = license;
  etc.
}
loader1.load("myphpscript.php");

Or if you can get it to run locally you don't even have to do that, just start flash in debug modus (shift enter) and set a breakpoint in onLoad function. Then have a look what variables are available when the script stops at your breakpoint. You just have to make sure that the loader1.load(url) - url has to be correct.
Whenever I try things like this from flash locally I have my webserver running and calll it like this: loader1.load("localhost/dsfg/script.php");
0
mrhellAuthor Commented:
Ok I just tried your suggestion and nothing came up in the text field. Just incase you need to know I tried both dynamic text box and input text box.
0
CalronCommented:
Ok, just to be on the save side, put the textfield on the main timeline and set it with:

_root.myfield.text = "asdfasdf";


if that still doesn't show anything then the load method is not called. But that seems strange, as you said that undefined was showing up in the textfields that you were filling with the input from the php script. That would mean that the onLoad function is being called but the results of the script are not arriving. That would point to a bad url in the loader1.load(url); call. Please check these things and get back to me.

If those things don't work, try to post your fla file and I'll have alook at it.
0
rascalpantsCommented:
Mrhell,

I need to see the page for myself...  please send me the proper link to the PHP page...  both links would be great...

rp
0
mrhellAuthor Commented:
I got the text field to print out "asdasda" but undefined still prints out in the other boxes. I checked my url and it's fine.
0
CalronCommented:
It really seems to be a problem of the php script. Please do post the link as rascalpants asked ...
0
mrhellAuthor Commented:
0
CalronCommented:
The script seems to work fine.

Please check the code that you are using to call the script:

loader1.load(url);  it should be something like this "userdata.php?username=myusername"  if the php script and the flash file are in the same location.
0
mrhellAuthor Commented:
I dont think that suggestion would work because I have set up a login for thousands of users to use, and if I put one username in then whenever a user logs in, it will print out the data for that one user.
I have my flash file on my work pc, and the script on my work server...would this make any difference?
0
CalronCommented:
that could be the problem. YOu have to use the excat same url in the load function as you have to to see the script result on your work pc. include http://

so it would be something like:  loader1.load("http://workserver/scripts/myphp.php?username=" + username);  in this case username would be a variable. It doesn't really matter where you get your username from. You just have to get it into the script somehow. If you are planning on so many users you either keep the name in flash or use something like sessions and store the username in the session and have the script get it out of there.

One thing to test this thing. I would return 0 instead of nothing as the default.
&username=0&license=0&wins=0&losses=0&team=0&balance=0  
I am not sure how flash interprets empty strings loaded in though they should not come up as undefined.

First check the exact url that you need to display the result from a browser on your work pc, then put that into load ...
0
mrhellAuthor Commented:
I've put in the exact url along with the username extension, but I am not sure how to put the username into the script.
0
rascalpantsCommented:
this url:

www.auto-salongame.com/scripts/userdata.php 

should have a username attribute attached to it...

what is the username that we can test?

www.auto-salongame.com/scripts/userdata.php?username=  ?


rp
0
CalronCommented:
First you have to decide whether to use get or post. To start just use the get method. The call it with this at the end:   ?username=myusername

in the script add this line:

$username =  $HTTP_GET_VARS["username"];
0
mrhellAuthor Commented:
Calron - When I tried that, then went to www.auto-salongame.com/scripts/userdata.php?username=*Rusty*
It came up with a phase error.
0
CalronCommented:
What exactly did you try? The $username =  $HTTP_GET_VARS["username"];  , you can go without that since you already are getting the username into the script.
0
rascalpantsCommented:
the problem that might solve everything is that you don't have a "&" at the end of your variable output...

it should look like this:

&username=*Rusty*&license=Pro&wins=5000&losses=0&team=Auto Salon Admins&balance=2147483647&


put an "&" at the end and then test it...

rp
0
mrhellAuthor Commented:
I have tried all your suggestions but for some reason it's not working.
If you post your email I will send you the fla, I can't put it on this site as my job would be put on the line for doing such a thing.
0
CalronCommented:
You could also make a stripped down version of your fla with just the textfields and the loading, that way you would not be giving out anything "secret".

my email:  tobias at lauchenauer.info
0
CalronCommented:
I have had a look at your file and the problem is acrually quite simple. Your textfields are in another scene. You can only write into textfields that are available, that is on the current frame.
So what you would have to do is store the data in a variable and then when you get to the scene where you require the data write it into the textfields.

Something like this should work:

var loader1 = new LoadVars();
loader1.onLoad = function() {
  _global.myuserdata = new Object();
  _global.myuserdata.license = license;
  _global.myuserdata.balance = balance;
  etc.
}
loader1.load("myphpscript.php");


then on the scene where you see the textfields you could use the following code to write value into the textfields:

balance = _global.myuserdata.balance;
etc.
0
CalronCommented:
One other thing I noticed in your fla, is that you don't give the textfields instance names. YOu use variables instead. There you just have to remember this difference:

instancename.text = "newtext";  for instance names
variablename = "newtext"; for using variable names

There is an advantage to using instancenames, but that only comes into account when you want to edit any other properties of the textfield. Using the variable you can only use change the text. Where with instancenames you can change the font, color, etc.
In your case you probably won't have to change any other properties of the textfield, so variables will work fine for you.
0
mrhellAuthor Commented:
Well I tried your first suggestion of using the _global.userData.balance = balance in one scene and have
balance = _global.userDataata.balance in my scene in which the data is printed but it still came up with undefiend.
The next thing I tried was putting _global.userData.balance = balance into my main scene and that printed absolutly nothing.
Any suggestions?


By the way I'll give you an A grade for all this help, thanks :)
0
CalronCommented:
_global.userData.balance = balance  

This will assign the value of balance to _global.userData.balance  so that will not do what you require. Let me test a few things and I'll get back to you. I didn't have much time yesterday so I just did a few quick checks.
0
CalronCommented:
I got it to work:

var userData = new LoadVars();
userData.onLoad = function() {
      _global.myuserdata = new Object();
      _global.myuserdata.license = this.license;    // you didn't need the $license as that is a php way to write a variable
      _global.myuserdata.wins = this.wins;           // I forgot about the this.  because I am normally a java programmer and there you don't need "this.---"
      _global.myuserdata.loses = this.losses;
      _global.myuserdata.team_name = this.team;
      _global.myuserdata.balance = this.balance;
      gotoAndStop("Main", 1)
}
userData.load("http://www.auto-salongame.com/scripts/userdata.php?username=*Rusty*");  // of course the suername can be added in a different way


and the first frame on Main:

stop();
license = _global.myuserdata.license;
wins = _global.myuserdata.wins;
losses = _global.myuserdata.losses;
balance = _global.myuserdata.balance;


Probably the best thing for the future is to set up a Player object where you store all the player data. Then have an instance of that on _global.
0
mrhellAuthor Commented:
For my loading scene I have this;

stop();
connect = "Loading...please wait...";
userData = new LoadVars();
var userData = new LoadVars();
userData.onLoad = function() {
    _global.userData = new Object();
    _global.userData.license = this.license;
    _global.userData.wins = this.wins;
    _global.userData.loses = this.losses;
    _global.userData.team_name = this.team;
    _global.userData.balance = this.balance;
      gotoAndStop("Main", 1)
}
userData.load("http://www.auto-salongame.com/scripts/userdata.php?username=*Rusty*")

For my main scene I have this;

stop();
license = _global.myuserData.license;
wins = _global.myuserData.wins;
loses = _global.myuserData.loses;
team_name = _global.myuserData.team_name;
balance = _global.myuserData.balance;

To my understanding this is the exact code that I should be using, however on my pc it still prints out undefiend.
0
CalronCommented:
The only thing I can see right now is this:

userData = new LoadVars();
var userData = new LoadVars();

you only need one or the other.

Oh and the second thing:

_global.userData  is what you are writing into
_global.myuserData  is what you are reading out of  

that should correct your thing
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CalronCommented:
Make sure that you are reading and writing to the same variables. So if you change your Main to

stop();
license = _global.userData.license;
wins = _global.userData.wins;
loses = _global.userData.loses;
team_name = _global.userData.team_name;
balance = _global.userData.balance;

you should be fine
0
mrhellAuthor Commented:
I'm totally stumped because my coding is now perfect yet it still prints out undefiend.
Would it be possible for you to email me the fla?
0
mrhellAuthor Commented:
Ok I've got everything to work except my problem of having multipul users, use this program...
0
CalronCommented:
Well, that's a different story .... you'll need some kind of client server thing to make that work ....

You might want to check this link:

http://www.amfphp.org/
0
CalronCommented:
It all depends what kind of game you want to set up. i.e. realtime races against each other where every move has to be transmitted, turn based things, etc.
0
mrhellAuthor Commented:
Thanks for your help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.