I had a hard time getting started with php, you can do alot of things with this.
Ok all of this is assuming you have something on the stage called text_txt and a submit button called bt_sub
sample
http://simpleheartmedia.com/tophp.swf
and source is attached to my post
I’ll do like kirupa does, post the whole code, then explain Ok, ready?
AS3
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
stop();
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("write.php");
var varLoader:URLLoader = new URLLoader;
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
bt_sub.buttonMode = true;
bt_sub.addEventListener(MouseEvent.CLICK, ValidateAndSend);
function ValidateAndSend(event:MouseEvent):void
{
// gotoAndPlay(2);
variables.wootext = text_txt.text;
varLoader.load(varSend);
}
var url:String = "test.txt";
var loadit:URLLoader = new URLLoader();
loadit.addEventListener(Event.COMPLETE, completeHandler);
loadit.load(new URLRequest(url+'?r='+String(Math.random()*9999));
function completeHandler(event:Event):void
{
text_txt.text = event.target.data as String;
}
Thats the AS3, go down, i’ll explain
<?php
$wootext = $_POST['wootext'];
// Strip slashes on the Local variables
$wootext = stripslashes($wootext);
$filename = 'test.txt';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'wb')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $wootext) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($wootext) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable, or doesn't exist";
}
?>
Ok Now i’ll explain the actionscript (the php is already commented)
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
stop();
That imports everything you need to load a URL and submit _POST data
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("write.php");
var varLoader:URLLoader = new URLLoader;
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
Creates a variable for the URL to _POST data to
Varsend, is pointing to your php file.
Varloader loads that URL
Varsend, again, sends _POST data
Varsend, once again, sends your data (in this case, text strings) in _POST data form
bt_sub.buttonMode = true;
bt_sub.addEventListener(MouseEvent.CLICK, ValidateAndSend);
function ValidateAndSend(event:MouseEvent):void
{
// gotoAndPlay(2);
variables.wootext = text_txt.text;
varLoader.load(varSend);
}
variables. is defining the variable as it will be shown in php (see php discription below)
In my case i named it ‘wootext’, this has to be consistant with the php file below.
Varloader.load(varsend) is sending your data to the php file via the variables above.
var url:String = "test.txt";
var loadit:URLLoader = new URLLoader();
loadit.addEventListener(Event.COMPLETE, completeHandler);
loadit.load(new URLRequest(url+'?r='+String(Math.random()*9999));
function completeHandler(event:Event):void
{
text_txt.text = event.target.data as String;
}
Thanks to Templarian for correcting the caching error
The variable ‘url’ is defining your text file path
The variable loadit is is creating a URL loader
Then adding an event listener for when the load has completed
Then displaying that text inside your input text box called text_txt
Now for the php, i’m not going to be completly taking it apart, but i’ll explain the basics.
<?php
always starts off a php file
$wootext = $_POST['wootext'];
// Strip slashes on the Local variables
$wootext = stripslashes($wootext);
gets the post data that was sent from flash remember that i named it wootext, it must be consistant with the as3 variable or it won’t do anything
Also its stripping slashes from the newly created text variable
$filename = 'test.txt';
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
Defines the text file you will be opening and writing to
if (!$handle = fopen($filename, 'wb')) {
echo "Cannot open file ($filename)";
exit;
}
Opens the variable filename, and opens it in ‘wb’ mode, that truncates the file to 0 length.
}
// Write $somecontent to our opened file.
if (fwrite($handle, $wootext) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($wootext) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable, or doesn't exist";
}
?>
the rest is for testing purposes basically, still keep it in therer though.
especially the lines with fclose in them.
and thats it