-
Axis Games
Getting data from PHP to Flash (HELP!)
I'm working on my own flash games website and I'm having some difficulty getting php variables into my .swfs.
I can go from .swf to PHP, but not the other way around. I'm hoping someone can figure out why it's not working.
Before I show my code, I have a question:
Does the .swf need to be embeded in the .php page that you are requesting data from?
Here's what is happening:
A user logs in by entering their user name and password.
The script checks to verify that that user exists in the database.
The script checks to verify that that user name and password combo match.
I now want the script to send Flash a variable inside that user's record, such as their user name, but nothing happens.
Here's the AS code:
Code:
my_lv = new LoadVars();
my_lv.load("http://www.axis-games.com/test.php");
my_lv.onLoad=function(success){
if(success){
_root.textfield=myvariable;
}else{
trace("Failed to load");
}
}
}
The code is placed on the first frame of the main timeline.
On the stage is one dynamic text field with the variable name _root.textfield.
The PHP code for test.php is:
Code:
<?
$username=$_POST['enteredName'];
echo "&myvariable=$username";
?>
//The .swf's <object> tags are located here
(enteredName is what the user entered as their log-in name).
When I log in, the dynamic text field in the .swf stays blank. I know that the variable "myvariable" is set to the "enteredName" variable, but I can't figure out where my problem is. Any help would be greatly appreciated.
-
-
Flashmatics
Also if you like you can have a look at my flash-php login example in my library...(make sure u use sessions for security)
-
Senior Member
you can also use sendAndLoad to return data back to FLASH with the same 'call'
-
Axis Games
Thanks for the info, I fixed that problem, but now I'm pretty sure I've got a syntax mix-up somewhere in my php code.
I'm getting data from the php page, and it's showing up in the right dynamic textfield, but instead of it being the user name, like I want, it's a bunch of HTML code.
Here's what I've got:
AS for the .swf:
Code:
my_lv = new LoadVars();
my_lv.load("http://www.axis-games.com/login.php","POST");
my_lv.onLoad=function(success){
if(success){
_root.status=my_lv.myvariable;
}else{
_root.status="not loaded";
}
}
Code for login.php:
Code:
|
|
html code
|
|
<?
$username=$_POST['username'];
$password=$_POST['password'];
print"&myvariable=$username";
$dbh=mysql_connect ("localhost", "<username>", "<password>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("<db name>");
$sqlquery="SELECT * FROM members WHERE username='$username' && password='$password'";
$result=mysql_query($sqlquery);
$number = mysql_num_rows($result);
if ($number < 1){
?>
<object> //the .swf I want if it's a failed login// </object>
<?
}
else {
?>
<object> //the .swf I want to display the "myvariable" variable// </object>
<?
}
?>
|
|
html code
|
|
For whatever reason, the myvariable variable is being set to everything after the else{ statement, and all of the code is being displayed in the text field inside the .swf.
-
as far as i can tell you not POSTing any data...that may have something to do with it.
-
Total Universe Mod
Yeah, you need sendAndLoad if you want to send data and do something with the results.
-
Axis Games
I tried changing "POST" to "GET" in the flash file, and I still got the same result. Thanks though. I'm willing to try anything.
-
//this is to load the return
my_lv = new LoadVars();
my_lv.onLoad=function(success){
if(success){
_root.status=my_lv.myvariable;
}else{
_root.status="not loaded";
}
}
//this is to send
my_sv = new loadVars()
my_sv.username = "josecuervo2006"
my_sv.sendAndLoad("http://www.axis-games.com/login.php",my_lv);
try something more like that
-
Axis Games
Originally Posted by jAQUAN
Yeah, you need sendAndLoad if you want to send data and do something with the results.
Will sendAndLoad work if I want to load the data into a different .swf than the one that I sent it from?
-
ummm not sure what you mean?
it returns data. you can do whatever you want with the data
-
Axis Games
www.axis-games.com
Take a look -- I've got multiple .swf files on the same .html/.php page. My question is, is sendAndLoad what I want if I'm sending PHP data from .swfA and I want to load the data into .swfB?
Example, someone enters their log-in information (top right corner of the page). That section is it's own .swf file. I want to be able to display their login name in another .swf file located on that page, or another page. Hope that make sense.
Last edited by Djugan; 07-20-2006 at 06:54 PM.
-
Total Universe Mod
As long as .swfA and .swfB are children of some parent .swf, yes you can do that.
I'd have the parent do the sendAndLoad and store the results so .swfB can access them when needed.
A sendAndLoad is just what it says, it sends data to a .php file, then loads the results the .php generates.
The PHP reads each property of the loadVars object as a var (either POST or GET depending on what you specified in your sendAndLoad command). You then have to make php send a response string that include the property you want to read after the echo.
Remember, you need two loadVars objects, one to add properties to for sending and one to read properties from when php responds.
-
no, use sessions for the username and password(a PHP feature, or maybe its a linux feature)
you could useSend and load and write it to a shared object that the other SWF then accesses, but that may have its own problems
If its just a login thing use sessions if its other stuff you may want to look into useing sendAndLoad with shared objects.
edit:
JaQuan its not it
<PHP/HTML PAGE>
<frame1 = swfa.swf>
<frame2 = swfb.swf>
<>
-
Axis Games
It's not absolutely necessary to send that paticular data back into flash; I could just as easily display the username variable in PHP/HTML.
However, the whole premise of the site is earning points to your account for playing the games, and then spending those points to unlock special features inside those games. If I can use sessions for that, I may go that route.
On the other hand, I've had some decent experience with Shared Objects. I think it would work if I write the variables to a shared objects file at the same time that I send the data to the PHP file, then just request those variables in another .swf after I've validated the login information. It sounds like it might work, but there's only one way to find out ;-).
Thanks for all your help, guys. If you think of anything else, please let me know.
-
Total Universe Mod
Here's a sendAndLoad example just in case you go that route.
flash code
Code:
function submitForm(){
//create a loadVars object for submitting
var submit_lv = new LoadVars();
//create a loadVars object for receiving results
var result_lv = new LoadVars();
//assign submit_lv's properties (variables)
submit_lv.pass = password_txt.text;
submit_lv.user = username_txt.text;
//prepare a response function for when the results arrive
result_lv.onLoad = function(success) {
// If Flash is able to successfully send and load the variables from the server-side script...
if (success) {
//from within the onLoad function, this refers to result_lv so its properties are this.property
if(this.response == 'valid'){
//this is a valid user
} else {
//if your PHP file is not found
output_txt.text = "Request Manager is not responding, Please Try Again.";
}
};
//this is performed last because result_lv's reactions need to be setup first.
submit_lv.sendAndLoad('recommend.php', result_lv, "POST");
}
PHP code
PHP Code:
$password = $_POST["pass"];
$username = $_POST["user"];
//some evaluation script
if($password == '12345' && $username == 'bob'){
$response = "valid";
}else{
$response = "invalide";
}
echo 'response='.$response;
exit;
Last edited by jAQUAN; 07-20-2006 at 07:35 PM.
-
then you would just create a session in PHP that would have the login/pass (plus whatever qualifier you want...just userdata)
then the game would do a load on the index.php and pull the session data and if the permissions were high enough it would allow you to play
-
Axis Games
Originally Posted by joran420
then you would just create a session in PHP that would have the login/pass (plus whatever qualifier you want...just userdata)
then the game would do a load on the index.php and pull the session data and if the permissions were high enough it would allow you to play
I don't know anything about sessions, I'm a PHP noob. I'll do some reserach on it and see what'll work best.
Again, thanks for all the info and help, guys.
-
I think you can find lots of tutorials...its not all that hard.
-
Axis Games
Bad news :-(. You can't open shared objects from different .swf files. I'm having the same problem as I did with PHP.
I write a SO file in one .swf, and I can open the data in it without a problem as long as I open it in that .swf, but if I try to open it in a different one, it returns as undefined.
I guess I'll keep searching for another way to do it. About the only thing I can think of is it redo the design of the site to have one giant .swf file instead of 8-9 smaller ones in frames/tables.
I'll be out of town for a day or two, but let me know if you guys have any insight. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|