hi... this is my first post ...
this is the code that i'm trying to build but error is there.
the flash is can't execute, so what should i do??
i need help.... please
package communication2{
import flash.display.MovieClip;
import flash.events.*;
import flash.text.TextField;
import flash.net.navigateToURL;
import fl.data.DataProvider;
// classes for server script connection
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.events.IOErrorEvent;
public class Communication2 extends MovieClip {
private var myRequest:URLRequest;
private var myVars:URLVariables;
private var myLoader:URLLoader;
// to keep things a little simpler, there is only traced error messages
// you may want to show the errors to the users...
// This code uses URLVariables for connection with a server
// in particular we connect through VARIABLES to PHP and MySQL as a database
// you can use the code to connect to TEXT and BINARY data too
public function Communication2() {
// set up the objects to send and receive data
myVars = new URLVariables();// this holds variables to send (as dynamic properties)
myRequest = new URLRequest();// this prepares the request for the loader
myRequest.url='http://www.yourserver.com/communication2.php';
myRequest.method=URLRequestMethod.POST;
myRequest.data=myVars;
myLoader = new URLLoader();// this is the loader that will send and receive
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;// could also be be TEXT or BINARY
myLoader.addEventListener(Event.COMPLETE, showGrid);
myLoader.addEventListener(IOErrorEvent.IO_ERROR, showError);
// get the initial data from PHP which gets the data from a database
getData();
// event on button to send submissions to the database
mySubmit.addEventListener(MouseEvent.CLICK, submitData);
}
function submitData(e:MouseEvent) {
if (firstName.text == "" || lastName.text == "" || fear.text == "") {
navigateToURL(new URLRequest("javascript:alert('Please enter all fields');"), "_self");
} else {
getData(firstName.text, lastName.text, fear.text);
firstName.text = lastName.text = fear.text = "";
}
}
function getData(myFirstName:String="", myLastName:String="", myFear:String="") {
myVars.firstName = myFirstName;
myVars.lastName = myLastName;
myVars.fear = myFear;
myLoader.load(myRequest);// this sends the variables and loads new data from php
}
private function showGrid(e:Event) {
// the data comes into the e.target.data property
// if the data is of the format URLLoaderDataFormat.TEXT then just use the data
// if the data is VARIABLES then convert the data into variable form like so:
var newVars:URLVariables = new URLVariables(e.target.data);
trace(newVars.simpleVariable);
if (newVars.error == 0) {
var myDataProvider:Array = [];
for (var i:uint=1; i<=newVars.total; i++) {
myDataProvider.push({firstName:newVars["firstName"+i], lastName:newVars["lastName"+i], fear:newVars["fear"+i]});
}
myGrid.columns = ["firstName", "lastName", "fear"];
myGrid.dataProvider = new DataProvider(myDataProvider);
myGrid.getColumnAt(0).headerText = "First Name";
myGrid.getColumnAt(0).width = 100;
myGrid.getColumnAt(1).headerText = "Last Name";
myGrid.getColumnAt(1).width = 100;
myGrid.getColumnAt(2).headerText = "Greatest Fear";
} else {
trace("There was an error in PHP");
}
}
private function showError(e:IOErrorEvent) {
trace("There was an error!");
}
}
}
#
# MySQL Table structure for table `fears`
# put this in the SQL box to quickly create the table
#
CREATE TABLE fears (
id int(11) NOT NULL auto_increment,
firstName text NOT NULL,
lastName text NOT NULL,
fear text NOT NULL,
date date NOT NULL default '0000-00-00',
PRIMARY KEY (id)
) TYPE=MyISAM;
<?php
$firstName = isset($_POST['firstName']) ? $_POST['firstName'] : "";
$lastName = isset($_POST['lastName']) ? $_POST['lastName'] : "";
$fear = isset($_POST['fear']) ? $_POST['fear'] : "";
$date = date("Y-m-d");
$error = 0;
// PRE-MAKE DATABASE
// go to phpmyadmin
// in a database called "database" - or whatever...
// login and make a table called "fears" with 5 fields
// id - make it an INT and autoincrementing and the key
// firstName - make TEXT
// lastName - make TEXT
// fear - make TEXT
// date - male Date
// OPEN DATABASE
// use your username and password
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("database",$db);
if ($firstName != "") {
// INPUT TO MYSQL
// create SQL query to insert any names and fears into the "fears" table
$query = "INSERT INTO fears (id, firstName, lastName, fear, date) VALUES ('', '$firstName', '$lastName', '$fear', '$date')";
// use PHP function to execute query. We use this function every time we
// want to access the database - for instance to add, delete or change something
$result = mysql_query($query,$db);
if (!$result) {
$error = "Sorry could not record fear";
}
}
// SERVER SCRIPT OUTPUT TO FLASH
// if we do not have to go to a database we can just send a variable to flash like so
echo "simpleVariable=simple value";
// OUTPUT TO FLASH
// this is how we handle sending variables back to Flash
// note that all name=value pairs are separated by &
$query = "SELECT * FROM fears";
$result = mysql_query($query,$db);
$total = mysql_num_rows($result);
$output = "";
if ($result) {
$i=0;
while ($myrow = mysql_fetch_array($result)) {
$i++;
$output .= "&firstName".$i."=".urlencode($myrow['firstName']).
"&lastName".$i."=".urlencode($myrow['lastName']).
"&fear".$i."=".urlencode($myrow['fear']);
}
} else {
$error = "Sorry could not access fears";
}
// send the data to Flash using the URL encoded format
echo "&total=".urlencode($total)."&error=".urlencode($error).$output;
?>
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/DataGrid.html#DataGrid()
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/