Groups | Blog | Home
all groups > flash actionscript > may 2005 >

flash actionscript : PHP and Flash - not communicating orrectly


yertari
5/20/2005 12:00:00 AM
Hi all,

I have a problem, that has been perplexing me for days of staring at the
screen. It concerns using PHP with flash. What has come refreshin whilst
learning php intergration with flash, is that i understnad how it works...which
is a first!...lol

i understand, that you echo('') a message back from the php page to tell flash
a particlar result, but i just cannot work out, why the information isnt being
sent back to flash.. Idealy i wish to use SendAndLoad, but because that doesnt
take you to a browser page, i am using Load instead, to when i go to the
destination PHP page, i can check out visually whether the page is being
accessed correctly.

My flash code is as follows:

_root.loggedIn=false;



sym.onEnterFrame=function()
{
_root.LoginApplication.Login.submit.onPress = function()
{


this._parent.username.text="test";
this._parent.pass.text="";
//connect to database
var ab = new LoadVars ();
ab.username = "test";
ab.pass = this._parent.pass.text;
//ab.onLoad= validate;
//ab.sendAndLoad ('http://www.jsten.com/ab.php', ab, "POST");

ab.send("http://www.jsten.com/ab.php", "POST");





function validate()
{
trace("hi");
if(this.output=='success')
{
trace("success");
sym._alpha=0;
}

if (this.output=="fail")
{
sym._alpha=0;
}


}
}
}


and simply on the psp page i have

<?php
session_start();



if ($_REQUEST['username']=='test' && $_REQUEST['pass']=='test')
{
echo 'output=success';
echo("The Php page has been connected correctly'
$_SESSION['flashanttestlogin']=TRUE;
}
else
{
echo 'output=fail';
$_SESSION['flashanttestlogin']=FALSE;
}
?>

I would be most apreciative is somone could have a look, and maybe shed a
little light on why this is not connecting correctly, and if i enter the
username 'test' and the password 'test' why PHP isnt seeing this, and thus
printing the meesage out.

Cheers,
Steve
SMakinson
5/20/2005 12:00:00 AM
yertari
5/20/2005 12:00:00 AM
Hi,

All i have on my ab.php page is:

<?php

echo($_POST['username']);

if ($_POST['username'] == "test")
{
echo 'output=success';

i included the echo('username'), to test whether the flash variable 'username'
was going to php, and thus if it is, this would print the username to screen,
but alas it doesnt. Am i not understanding how it works??

Could u please provide an example of a little code in flash, that would call a
php file, and display the username or varaiable send from flash onthe php page
- tp show that the sending of the variables does indeed work.

Thanks

Steve
}
?>
SMakinson
5/20/2005 12:00:00 AM
I can show you that your code does load your page o:
I am attaching the slightly modified code that I copied from your post and he
results when running your code in the output window.





CODE:
------------------------>
var ab = new LoadVars();
ab.username = "MyUserName";
ab.pass = "MyPassword";
ab.onLoad= validate;
ab.sendAndLoad ('http://www.jsten.com/ab.php', ab, "POST");
//ab.send("http://www.jsten.com/ab.php", "POST");
function validate(suc) {
trace("Data Loaded!!!");
trace("");
trace("-------------------");
trace("");
trace(unescape(this));

if (this.output == 'success') {
trace("success");
sym._alpha = 0;
}
if (this.output == "fail") {
sym._alpha = 0;
}
}


RESULTS:
------------------>

Data Loaded!!!

-------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>jsten.co.uk/index</title>
<meta name="keywords" content="js10,js 10, jsten,js ten,trance,">
<meta name="jays 10" content="js ten online">
<meta name="js 10" content="js ten online">
<meta name="js ten" content="js ten online">
<meta name="jay s 10" content="js ten online">
</head>
<frameset rows="100*" border="0" frameborder="no" framespacing="0">
<frame name="site" src="http://www.jsten.co.uk/ab.php" marginwidth="0"
marginheight="0" noresize scrolling="auto">
<noframes>
<body bgcolor="#ffffff">
<p></p>
</body>
</noframes>
</frameset>
</html>
&onLoad=[type Function]&pass=MyPassword&username=MyUserName
yertari
5/20/2005 12:00:00 AM
Thanks kindly, for helping me out....
Gonna run through it now

SMakinson
5/20/2005 12:00:00 AM
yertari
5/20/2005 12:00:00 AM
Hi,

As i have been trying to work with php, and not familiar at this moment in
time, with passing to html files, can you explain this line please:

&onLoad=[type Function]&pass=MyPassword&username=MyUserName

and how i would actually get the php file to acknowledge that indeed the
username and password from the flash file have been received and accepted and
sent back to flash so i can act on the result

With each post....ur replying too, the idealogy is getting clearer to me.. so
thankyou

Steve

SMakinson
5/20/2005 12:00:00 AM
That line is part of the LoadVars object in flash from when this line was run:
trace(unescape(this));

It sounds like you are properly writing the php code, but I am wondering where
that html is coming from. If I goto your php page in a browser and view the
source I also see that html.
LuigiL
5/20/2005 12:00:00 AM
Start with a simple php-file and in your fla trace if you receive the variable:



var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function(success:Boolean){
if (success){
//perform action here
trace(result_lv.output);
} else {
//perform action here
trace("Error connecting to server.");
}
};
var send_lv:LoadVars = new LoadVars();
send_lv.username = "test";
send_lv.sendAndLoad("http://www.jsten.com/ab.php", result_lv, "POST");

<?php
//echo($_POST['username']);
if ($_POST['username'] == "test")
{
print "output=success";
}
?>
yertari
5/20/2005 12:00:00 AM
Hi,

Well.. i am surprised as you mate......

I havent a clue what that page is, and i never written it.... very very
strange indeed.

the ab.php page in dreamweaver actually reads:

<?php

echo($_POST['username']);

if ($_POST['username'] == "test")
{
echo 'output=success';
}
?>


..as short as that! - with none of the code that we have both seen. Based on
the code above, which simply prints on the php page a confirmation that the
password from flash is indeed corect, do you think the reason its not working,
is because of all that rubbish.. that is coming up when we both press VIEW
SOOURCE?


SMakinson
5/20/2005 12:00:00 AM
Just to be sure, you are in the code view in dreamweaver when you are adding php code, right?

Add another echo outside the if so we can see if that appears.
something like:
LuigiL
5/20/2005 12:00:00 AM
Well, you say you don't understand why the information isn't send back to
Flash. So, I'm showing you how to use the loadVars class and specifically the
sendAndLoad method.
Put the php-file online, create a new Flash-movie, attach the actionscript to
frame 1 and test your movie. Once you have grip on a simple setup with one
var... the rest will follow.
Success.
David Powers
5/20/2005 12:00:00 AM
[quoted text, click to view]

For an in-depth tutorial on communicating between Flash and PHP, go to
my site at http://computerbookshelf.com/php5flash/ and follow the link
for the sample chapter. It's a 39 page PDF that includes two fully
worked examples of using LoadVars.load and LoadVars.sendAndLoad. It also
shows you how to check that variables are being received correctly by PHP.

--
David Powers
Author, "Foundation PHP 5 for Flash" (friends of ED)
Co-author "PHP Web Development with DW MX 2004" (Apress)
yertari
5/20/2005 12:00:00 AM
Thanks all for your help - really apreciated.
I am gonna try the ideas presented here now
yertari
5/20/2005 12:00:00 AM
Hi,

Thanks for the reply.

I figured out what it was and it doesnt really make any sense. My domain is
registered as jsten.co.uk, but i have also jsten.com which directs to it.

You will notice in the url, that i am using jsten.com.... and this is what
caused the problem. I changed it to jsten.co.uk, and now i can access the php
fine.

This must be some kind of bug...i will contact my host on Monday, to ask why
using com would be causing a problem

Thanks
Steve
AddThis Social Bookmark Button