PDA

View Full Version : Newbie tutorial - AS3 + PHP - Notepad



saxx
August 15th, 2008, 07:21 AM
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 :)

Templarian
August 15th, 2008, 10:25 AM
Small Error with Cache can be simply fixed like below.


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

//edit, I think thats right I have a small understanding of AS

saxx
August 15th, 2008, 10:31 AM
Ah, thanks alot ^^ i'll update it

ajcates
August 21st, 2008, 04:25 AM
Left ya with a nice hello message.

spooks222
September 22nd, 2008, 10:51 AM
hmm, i don't understand how actionscript and php converse with each other. or is it just that the actionscript is calling the write.php file?

using your files, i can't get the words to save to my text file. everything else seems to be working fine though

saxx
September 23rd, 2008, 02:28 AM
It converses with php just like html does, with POST and GET variables. Anything specific you need help with?

arosemena
July 20th, 2009, 02:52 AM
what about sending the vars as post instead of using get

stras77
May 22nd, 2010, 09:38 PM
If any1 sees this please send me a "SIMPLE" code, on how to send "ONE" text over the post.
I learn nothing when something is smashed up against 100 of lines og code that i dont need.

All i want to do is when the flash file loads, i get sent to index.php with a $_post['foo'] that = 100. Noone seams to be able to explain this without making 10 email sends and a create xml smahed up against a check for errors and a system created to make homepages without human help.

My E-mail is: Stras77@gmail.com.

zarif
April 26th, 2011, 08:14 AM
Hi ,

please can you tell me how to make this in asp.net ?