Skip to main content
dalvydasv27776233
Inspiring
April 16, 2018
Answered

From AS3 write STRING to txt file with PHP

  • April 16, 2018
  • 1 reply
  • 2322 views

I have problem.

My AS3 code:

submit_btn.addEventListener(MouseEvent.CLICK, writeLine);

function writeLine(e:MouseEvent):void{

    var my_vars:URLVariables = new URLVariables();

    my_vars.kazkas1_t = kazkas1.text;

    my_vars.kazkas2_t = kazkas2.text;

    my_vars.kazkas3_t = kazkas3.text;

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = my_vars;

    var my_loader:URLLoader = new URLLoader();

    my_loader.dataFormat = URLLoaderDataFormat.VARIABLES;

    my_loader.load(my_url);

    kazkas3.text = "Išsaugota";

}

and PHP code:

<?php

$myfile = fopen("test.txt", "a")

$current = ($_POST['kazkas1_t'] . " " . $_POST['kazkas2_t'] . $_POST['kazkas3_t'] .'\n');

fwrite($current .'\n');

?>

But i GET error:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

    at Error$/throwError()

    at flash.net::URLVariables/decode()

    at flash.net::URLVariables()

    at flash.net::URLLoader/onComplete()

WHAT IM DOING WRONG?

This topic has been closed for replies.
Correct answer Robert Mc Dowell

Now i don`t know witch answer to mark as correct:)?


correct answer:

function writeLine(e:MouseEvent):void{

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = "kazkas1_t="+kazkas1.text+"&kazkas2_t="+kazkas2.text+"&kazkas3_t="+kazkas3.text;

    var my_loader:URLLoader = new URLLoader();
  
try {
      
my_loader.load(my_url);
  
} catch (error:Error) {
       trace
("Unable to load requested document.");
  
}

}

private function configureListeners(dispatcher:IEventDispatcher):void {
  dispatcher
.addEventListener(Event.COMPLETE, completeHandler);
  dispatcher
.addEventListener(Event.OPEN, openHandler);
  dispatcher
.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  dispatcher
.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  dispatcher
.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
  dispatcher
.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}

private function completeHandler(event:Event):void {
  
var loader:URLLoader = URLLoader(event.target);
  trace
("completeHandler: " + loader.data);
}

private function openHandler(event:Event):void {
  trace
("openHandler: " + event);
}

private function progressHandler(event:ProgressEvent):void {
  trace
("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}

private function securityErrorHandler(event:SecurityErrorEvent):void {
  trace
("securityErrorHandler: " + event);
}

private function httpStatusHandler(event:HTTPStatusEvent):void {
  trace
("httpStatusHandler: " + event);
}

private function ioErrorHandler(event:IOErrorEvent):void {
  trace
("ioErrorHandler: " + event);
}

---------------

my php:

<?php

$fp = @fopen("test.txt", "a");

if($fp){

  $current = $_POST['kazkas1_t'] . $_POST['kazkas2_t'] . $_POST['kazkas3_t'] ."\n";

    if(@fwrite($fp,$current)){

        // ok

    }else{

        // debug issue

    }

    fclose($fp);

}else{

     // answer to flash by something like varNamer=error to tell the flash client that something wrong happen

     // or send and email to yourself to debug it

}

?>

1 reply

Robert Mc Dowell
Legend
April 16, 2018

do you manage COMPLETE loader event?

dalvydasv27776233
Inspiring
April 16, 2018

Hm these scrips is all what I have.

Robert Mc Dowell
Legend
April 16, 2018

try this and tell me if it works. Also use HTTPS instead of HTTP in production mode or anyone can catch your data on the fly

function writeLine(e:MouseEvent):void{

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = "kazkas1_t="+kazkas1.text+"&kazkas2_t="+kazkas2.text+"&kazkas3_t="+kazkas3.text;

    var my_loader:URLLoader = new URLLoader();
  
try {
      
my_loader.load(my_url);
  
} catch (error:Error) {
       trace
("Unable to load requested document.");
  
}

}

private function configureListeners(dispatcher:IEventDispatcher):void {
  dispatcher
.addEventListener(Event.COMPLETE, completeHandler);
  dispatcher
.addEventListener(Event.OPEN, openHandler);
  dispatcher
.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  dispatcher
.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  dispatcher
.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
  dispatcher
.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}

private function completeHandler(event:Event):void {
  
var loader:URLLoader = URLLoader(event.target);
  trace
("completeHandler: " + loader.data);
}

private function openHandler(event:Event):void {
  trace
("openHandler: " + event);
}

private function progressHandler(event:ProgressEvent):void {
  trace
("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}

private function securityErrorHandler(event:SecurityErrorEvent):void {
  trace
("securityErrorHandler: " + event);
}

private function httpStatusHandler(event:HTTPStatusEvent):void {
  trace
("httpStatusHandler: " + event);
}

private function ioErrorHandler(event:IOErrorEvent):void {
  trace
("ioErrorHandler: " + event);
}