And the php file
check.php
<?
$users = array(
'iamthwee' => 'password',
'IIIIIamthwee' => 'password',
'Immthwee' => 'password');
$found = false;
foreach($users as $un => $pw) {
if ($_POST['username']==$un && $_POST['password']==$pw)
{
$found = true;
break;
}
}
if (!$found) echo '&valid=0&';
?>
Flash, the echo string is made up of variable=value pairs, surrounded by &s. Flash itself will format this returned data for display, within the LoadVars onLoad routine.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
^Scrap all the above. It's not actionscript 3.0 That'll teach me to debug it first.
OK
flash.as
function completeHandler(evt:Event) {
var username = evt.target.data.username;
//var email = evt.target.data.email;
msg.text = username;
}
enterbtn.addEventListener(MouseEvent.CLICK, clickMe);
function clickMe(evt:MouseEvent):void
{
var request:URLRequest = new URLRequest("check.php");
request.method = URLRequestMethod.GET;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);
}
check.php
<?php
$returnVars = array();
$returnVars['username'] = "John Doe";
$returnVars['email'] = "johndoe@hotmail.com";
//send variables back to Flash
echo 'username=iamthwee'
?>
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
A quick example demonstrating the reverse flash -> php
flash.as
var myData:URLRequest = new URLRequest("some.php");
myData.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.title = "Hi friends";
variables.comments = 5;
variables.image = "joke";
myData.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myData);
function dataOnLoad(evt:Event){
trace(loader.data.status) //status is a custom flag passed from back-end
}
some.php
<?php
//Capture data from $_POST array
$title = $_POST['Title'];
$comments = $_POST['Comments'];
$image = $_POST['Image'];
echo $title;
?>
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
A more useful example...
Of sending variables to flash, querying the database and then DISPLAYING the RESULT back in flash.
Flash file
btnOK.addEventListener(MouseEvent.CLICK, clickMe);
//btnnext.addEventListener(MouseEvent.CLICK, clickMe2);
function clickMe(evt:MouseEvent):void
{
var myData:URLRequest = new URLRequest("some.php");
myData.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.name = usertxt.text;
variables.password = passwordtxt.text;
myData.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myData);
}
function dataOnLoad(evt:Event)
{
label1.text = evt.target.data.user;
}
php file
<?php
//Capture data from $_POST array
$name = $_POST['name'];
$password = $_POST['password'];
$conn = mysql_connect("localhost","tonic6","password");
mysql_select_db("tonic6_test",$conn);
$sql = "SELECT password FROM user where name ='".$name ."'";
$result = mysql_query($sql,$conn);
$pass = "";
while($newArray = mysql_fetch_array($result))
{
//give name to the fields
$pass = $newArray['password'];
}
if ($pass == $password)
{
echo "user=1";
}
else
echo "user=0";
?>
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439