PHP and flash

iamthwee 0 Tallied Votes 424 Views Share
// create two new instances of LoadVars, one to send and one to receive data
var dataOut:LoadVars = new LoadVars();
var dataIn:LoadVars = new LoadVars();

// define what should happen when the response is received,
// using 'this' to refer to dataIn and get data from it
dataIn.onLoad = function() {
   if (this.valid=="1") {
      msg.text = 'Welcome. ' ;
   } else {
      msg.text = 'Sorry';
   }
}

// the function that will be called by the Enter button 
function checkUser():Void 
{
   dataOut.username = username.text;
   dataOut.password = password.text;
   dataOut.sendAndLoad("check.php", dataIn, "POST");
}

// define the behavior of the Enter button
enterbtn.addEventListener("click", checkUser);
An example showing flash communicating with php and then back again to flash.

Actionscript 3.0 used.
Member Avatar for iamthwee
iamthwee

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.

Member Avatar for iamthwee
iamthwee

^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'

?>
Member Avatar for iamthwee
iamthwee

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;

?>
Member Avatar for iamthwee
iamthwee

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";
 


?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.