Solved

Getting php variables into dynamic text boxes

Posted on 2003-10-21
37
194 Views
Last Modified: 2010-04-03
<?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
0
Comment
Question by:mrhell
  • 18
  • 15
  • 4
37 Comments
 
LVL 5

Expert Comment

by:Calron
ID: 9591332
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
 

Author Comment

by:mrhell
ID: 9599010
I put that in and in all my boxes and "undefiened" prints out in them even though I have data in the database...
0
 
LVL 22

Expert Comment

by:rascalpants
ID: 9600965
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Calron
ID: 9601663
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
 

Author Comment

by:mrhell
ID: 9606385
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
 
LVL 5

Expert Comment

by:Calron
ID: 9606462
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
 

Author Comment

by:mrhell
ID: 9606517
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
 
LVL 5

Expert Comment

by:Calron
ID: 9606563
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
 
LVL 22

Expert Comment

by:rascalpants
ID: 9606634
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
 

Author Comment

by:mrhell
ID: 9606653
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
 
LVL 5

Expert Comment

by:Calron
ID: 9606697
It really seems to be a problem of the php script. Please do post the link as rascalpants asked ...
0
 

Author Comment

by:mrhell
ID: 9606733
0
 
LVL 5

Expert Comment

by:Calron
ID: 9606783
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
 

Author Comment

by:mrhell
ID: 9606812
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
 
LVL 5

Expert Comment

by:Calron
ID: 9606881
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
 

Author Comment

by:mrhell
ID: 9606941
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
 
LVL 22

Expert Comment

by:rascalpants
ID: 9606970
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
 
LVL 5

Expert Comment

by:Calron
ID: 9606981
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
 

Author Comment

by:mrhell
ID: 9606983
0
 

Author Comment

by:mrhell
ID: 9607054
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
 
LVL 5

Expert Comment

by:Calron
ID: 9607161
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
 
LVL 22

Expert Comment

by:rascalpants
ID: 9607183
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
 

Author Comment

by:mrhell
ID: 9607219
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
 
LVL 5

Expert Comment

by:Calron
ID: 9607272
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
 
LVL 5

Expert Comment

by:Calron
ID: 9607626
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
 
LVL 5

Expert Comment

by:Calron
ID: 9608010
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
 

Author Comment

by:mrhell
ID: 9612476
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
 
LVL 5

Expert Comment

by:Calron
ID: 9612497
_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
 
LVL 5

Expert Comment

by:Calron
ID: 9612640
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
 

Author Comment

by:mrhell
ID: 9612676
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
 
LVL 5

Accepted Solution

by:
Calron earned 50 total points
ID: 9612700
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
 
LVL 5

Expert Comment

by:Calron
ID: 9612710
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
 

Author Comment

by:mrhell
ID: 9612742
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
 

Author Comment

by:mrhell
ID: 9613037
Ok I've got everything to work except my problem of having multipul users, use this program...
0
 
LVL 5

Expert Comment

by:Calron
ID: 9613065
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
 
LVL 5

Expert Comment

by:Calron
ID: 9613105
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
 

Author Comment

by:mrhell
ID: 9613128
Thanks for your help :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Accessing Live Dynamic Stream 6 207
Flash player 17 install script .bat or .vbs 9 578
Firefox fullscreen video lags 4 175
Flash Player Action Script Error 1090 5 224
I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question