# Newbie tutorial - AS3 + PHP - Notepad

**URL:** <https://forum.kirupa.com/t/newbie-tutorial-as3-php-notepad/268568>\
**Category:** open source\
**Created:** [August 15, 2008, 10:21am UTC](https://forum.kirupa.com/t/newbie-tutorial-as3-php-notepad/268568 "2008-08-15T10:21:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![saxx](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@saxx](https://forum.kirupa.com/u/saxx)\
**Post date:** [August 15, 2008, 10:21am UTC](https://forum.kirupa.com/t/newbie-tutorial-as3-php-notepad/268568/1 "2008-08-15T10:21:40Z")

</div>

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](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**

```auto

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

<?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)

```auto
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

```auto

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

```auto
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.

```auto

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

<?php

```

always starts off a php file

```php

$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

```php

$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

```php

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.

```php

    }

    // 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 🙂
