navigateToURL sending data via POST to php page
actionscript-3, flash, php
Solution
in AS 3 the URLRequest class you use to specify your request has a method property which can be used to set he HTTP option for submission method, you'll need to set it to POST using URLRequestMethod constant POST for perfect form or you could use a "POST" string.
You can find a comprehensive example on snipplr
so in a nutshell:
var url:String = "http://localhost/myPostReceiver.php";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
requestVars.foo = "bar";
// ... fill in your data
request.data = requestVars;
request.method = URLRequestMethod.POST;
// after this load your url with an UrlLoader or navigateToUrl
When using Adobe Air You'd need to use the URLLoader class instead of navigateToURL() because of the following tidbit:
Parameters request:URLRequest — A URLRequest object that specifies the URL to navigate to.
For content running in Adobe AIR, when using the navigateToURL() function, the runtime treats a URLRequest that uses the POST method (one that has its method property set to URLRequestMethod.POST) as using the GET method.
Basically whenever you want to use POST set method correctly, as also indicated by the documentation for navigateToUrl:
next in php you'd be receiving the variable in the superglobal $_POST array, where you could access it as such:
<?php
$foo = $_POST['foo'];
/* $foo now contains 'bar'
assignment to another value is not necessary to use $_POST['foo']
in any function or statement
*/
Problem
Imagine that I have a form in a flash application with two fields, input1 and input2. And when the user finish filling this form, it goes to a php page. At the moment, I'm using the `$_GET` method to send the data. Like this: ``` var request:URLRequest; request = new URLRequest("http://site.com/page.php?data1="+input1.text+"&data2="+input2.text); navigateToURL(request); ``` And in the php code: ``` $_GET["data1"]; $_GET["data2"]; ``` But this way, the information stays in the URL. How can I send this via `$_POST`?