Here is a very simple example of two way communication with database using Flex and PHP. In this example, we are sending username and password to the PHP file from Flex. PHP file then validates the input and returns the appropriate response.
This example also demonstrates the simple PHP script to establish a database (MySQL) connection and validate username and password against the table in database.
In AS3, we can use flash.net.URLLoader, URLRequest and URLVariables class to send and load data. First create a class named SendAndLoadExample.
Class SendAndLoadExample:
package {
import flash.events.*
import flash.net.*;
public class SendAndLoadExample {
public function SendAndLoadExample() {}
public function sendData(url:String, _vars:URLVariables):void {
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
request.data = _vars;
request.method = URLRequestMethod.POST;
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(request);
}
private function handleComplete(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("Par: " + loader.data.par);
trace("Message: " + loader.data.msg);
}
private function onIOError(event:IOErrorEvent):void {
trace("Error loading URL.");
}
}
}
Now, create an object of SendAndLoadExample class in Flex.
SendAndLoadExample.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical">
<mx:Script>
<![CDATA[
import flash.net.URLVariables;
private var mySendAndLoadExample:SendAndLoadExample;
mySendAndLoadExample = new SendAndLoadExample();
private function sendAndLoad():void {
var url:String = "http://[your server]/login.php";
var variables:URLVariables = new URLVariables();
variables.UserName = "tushar";
variables.Password = "my_password";
mySendAndLoadExample.sendData(url, variables);
}
]]>
</mx:Script>
<mx:Button label="Fetch data" click="sendAndLoad()"/>
</mx:Application>
PHP file for login check: login.php
<?
$clientUserName=$_POST['UserName'];
$clientPassword=$_POST['Password'];
//////////////////////////////////////
// Host name
$host="[your server]";
// Mysql username
$username="[MySql database username]";
// Mysql password
$password="[MySql database password]";
// Database name
$db_name="[MySql database name]";
// Table name
$tbl_name="[MySql table name having usernames and passwords]";
function makeConnection() {
// Connect to server and select databse.
mysql_connect("$GLOBALS[host]", "$GLOBALS[username]",
"$GLOBALS[password]")or die("cannot connect");
mysql_select_db("$GLOBALS[db_name]")or die("cannot select DB");
}
function fireQuery($query) {
$result=mysql_query($query);
return $result;
}
function printOutput($code, $msg){
print "par=$code&msg=$msg";
}
//////////////////////////////////////
function checkUserID($id, $password) {
$sql="SELECT * FROM $GLOBALS[tbl_name] WHERE
userName='$id' and password='$password'";
$result=fireQuery($sql);
$count=mysql_num_rows($result);
if($count==1){
return true;
}
return false;
}
function init(){
if(isSet($GLOBALS["clientUserName"])
&& isSet($GLOBALS["clientPassword"])){
makeConnection();
if(checkUserID($GLOBALS["clientUserName"]
, $GLOBALS["clientPassword"])){
printOutput("1", "Login successful.");
} else {
printOutput("0", "Failed to login $GLOBALS[clientUserName].");
}
} else {
printOutput("0", "Required parameters missing.");
}
}
init();
?>
fin.
You might also like
| Using ActionScript 3.0 with PHP Loading External Variables Creating dynamic websites which combine the power of Flash or Flex with PHP is easier than ever with ActionScript... | Sample Loading Text and XML with URLLoader In previous versions of ActionScript, there were a couple of classes who had the capability of loading external... | Loading Text and XML with URLLoader in AS3 In previous versions of ActionScript, there were a couple of classes who had the capability of loading external... | Sending Data from AVM2 (AS3) to AVM1 (AS2/AS1) In Chapter 13, we showed how to use a LocalConnection to communicate between an AS3-based movie in Flash Player's... |





