Sending AS3 variables to and email using PHP - actionscript-3

I'm working on a flash content on my website that contains an input box and a submit button. The user should put an answer of a question in the input box and when he clicks on submit, the answer should be sent to an email address. The problem is, when a user enters an answer, I receive an email that contains :
array (
)
. Here are my codes:
AS3 Code:
var myData:URLVariables = new URLVariables();
myData.answer = answer.text;
var myRequest:URLRequest = new URLRequest("example.php");
myRequest.data = myData;
myRequest.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
The button's code:
myButton.addEventListener(MouseEvent.CLICK, sen) ;
function sen(Event:MouseEvent):void
{
navigateToURL( new URLRequest("example.php"), "_self");
}
The PHP code:
<?php
$text = var_export($_POST, true);
$to = "webhosting4#outlook.com";
$subject="Message from php";
mail($to,$subject,$text,"Content-Type: text/plain; charset=utf-8");
?>
So, what am I doing wrong?

myButton.addEventListener(MouseEvent.CLICK, sen) ;
function sen(Event:MouseEvent):void
{
var myData:URLVariables = new URLVariables();
myData.answer = answer.text;
var myRequest:URLRequest = new URLRequest("example.php");
myRequest.data = myData;
myRequest.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load( myRequest );
};

Related

Sending script to php file with URLLoader returns 0 when I try to GET it

I have a game, I want to write score in my database when the game is over. This is the urlloader function:
public function gameFinished(){
var urlLoader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("/../../sendScoreUnit4.php");
var requestVars:URLVariables = new URLVariables();
requestVars.score = total_score_hard;
req.data = requestVars.score;
req.method = URLRequestMethod.POST;
urlLoader.load(req);
}
My sendScoreUnit4.php file tries to get the score like so :
$score = (int)$_GET['score'];
but when I echo it I get 0 every time.
Any help would be appreciated.

Cancel a POST request

I have an app that makes a request to a server to generate an audio file and the problem that I have is that if I send two distinct and consecutive requests, I can not cancel the first. I tried with .unload or .unloadAndStop but always gives me error.
Any suggestions?
elfich="sonido" + elfichnum;
var request:URLRequest = new URLRequest();
var variables:URLVariables = new URLVariables();
var loader:URLLoader = new URLLoader();
variables.text=caja_texto;
variables.fileName=elfich;
request.url = "http://192.168.0.19:800/cgi-bin/tts.cgi";
request.method = URLRequestMethod.POST;
request.data = variables;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, cargacompletanew);
loader.addEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.load(request);
}
function cargacompletanew(event:Event):void
{
xlocalSound = new Sound();
var xreq:URLRequest = new URLRequest("http://192.168.0.19:800/output/" + elfich + ".mp3");
xlocalSound.load(xreq);
xlocalSound.addEventListener(Event.OPEN, iniciarSonido);
}
What about
loader.removeEventListener(Event.COMPLETE, cargacompletanew);
loader.removeEventListener(IOErrorEvent.IO_ERROR, onErrornew);
loader.close();
I think there was a problem with close() if you were trying to close it if nothing was loaded, so I'd try to put that inn try/catch block.
try
{
loader.close();
}
catch(e:Error)
{
loader("Oh noes!");
}

AS3 - navigateToURL issue

I have below code:
botton_1.addEventListener(MouseEvent.MOUSE_UP, onClick);
function onClick(e:MouseEvent):void {
var url:String = "http://www.xxxxxxxxxx.com/signInDetail";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.query = ta.text;
variables.packageId = 1;
variables.update = "askADoctorSaveQuery";
variables.task = "CHAT_PRE_ACTIONS_WITHOUT_PACKAGEID";
variables.query = ta.text;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE,compleHandler);
loader.load(request);
}
function compleHandler(event:Event):void {
var click_url2:String = "http://www.xxxxxxxx.com/ask-doctor-online";
var request2:URLRequest = new URLRequest(click_url2);
navigateToURL(request2, "_blank");
}
After the first request is loaded and compleHandler is called, the navigateToURL method opens the url in a popup instead of new tab. I am not sure what is the issue.
PLease help...
Replace this line. This loads it into a new window.
navigateToURL(request2, "_blank");
You have two other options:
In parent frame :
navigateToURL(request2, "_parent");
Replace current page :
navigateToURL(request2, "_self");
EDIT
After rechecking, I believe opening a window in a new tab is a user preference for the browser. There seems to be no way that a developer can control this. In IE 8 for example:
http://www.computershopper.com/feature/50-windows-7-tips-tweaks-and-secrets/open-a-link-in-a-new-tab-in-internet-explorer-8
navigateToURL(request2, "_self");

LocalConnection var into URLRequest

i use LocalConnection between two swf in the same page.
What i'm trying to do is to use a String sent from one swf to the other, as variable into my URLRequest...
So i can trace "myVar" into the function chemin, but i didn't find how to use it into URLRequest
thank's for your help
swf that receive the var :
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
function chemin(myVar:String){
trace(myVar)
}
var chargementXML:URLLoader = new URLLoader();
var fichier:URLRequest = new URLRequest(myVar);
Some references to help you out.
URLRequest.data: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.6#data
URLVariables: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html
So, if you want to pass some variables in your request, you do the following:
Create new URLVariables() object
Assign it's reference to URLRequest.data field.
var fichier:URLRequest = new URLRequest();
var urlVars:URLVariables = new URLVariables();
urlVars["myVarName"] = myVar;
fichier.data = urlVars;
This way your server side app will recieve a variable myVarName with value of myVar.
var lc:LocalConnection=new LocalConnection();
lc.client=this;
lc.connect("callBig");
var receivedVar:String = "";
// somewhere else
function chemin(myVar:String){
receivedVar = myVar;
}
// later
var chargementXML:URLLoader = new URLLoader();
var vars:URLVariables = new URLVariables();
vars.myVar = receivedVar;
chargementXML.data = vars;
var fichier:URLRequest = new URLRequest(myVar);

Flash / App Engine request data encoding problem (GET works, POST fails)

I'm new to Flash and trying to communicate with an app engine server but I'm having some encoding problems.
var playerLoader:URLLoader = new URLLoader();
playerLoader.addEventListener( Event.COMPLETE, function(e:Event) { doStuff(); } );
var requestVars:URLVariables = new URLVariables();
requestVars.q = "åäö";
var urlRequest:URLRequest = new URLRequest( serverUrl );
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = requestVars;
playerLoader.load( urlRequest );
This does not work, gives me some encoding errors on the server side. But if I switch POST to GET it automagically works.
Any clues what's going on and how I could use POST?
Thanks!
As far as I know, Actionscript's HTTP requests will natively be encoded in UTF-8.
I have it working this way for spanish chars:
var url:String = "historia.php";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
var now:Date = new Date();
requestVars.historia = "áóíúéñÁÓÍÚÉÑ";
request.data = requestVars;
request.method = URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.load(request);
And on the PHP side (resumed for clarity):
header ('Content-type: text/html; charset=utf-8');
if(isset($_POST['historia']) {
$historia = mysql_real_escape_string(utf8_decode($_POST['historia']));
$historia = filter_var($historia, FILTER_SANITIZE_STRING);
// insert into DB...
}
I use utf8_decode() because the database is not configured with encoding UTF-8.
How are you handling the request on the server side? Maybe the problem lies on the server side.