When rotating the script for Adobe Flash Professional IDE instead of the browser the program will convert POST
for GET
when to use navigateToURL(...);
, for this reason you will never get the VALOR DE TESTE
because he forces it to use GET
Using POST with navigateToURL
Note that to use POST
is necessary (in addition to req.method = URLRequestMethod.POST;
)
- Change the
content-type
for req.contentType = "application/x-www-form-urlencoded";
- And encode the variables like this
variables.valor = encodeURIComponent("VALOR DE TESTE");
It should look something like:
var url:String = "http://localhost/post-test.php";
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.valor = encodeURIComponent("VALOR DE TESTE");
req.method = URLRequestMethod.POST;
req.contentType = "application/x-www-form-urlencoded";
req.data = variables;
navigateToURL(req, "_blank");
So that navigateToURL
work with POST
you will have to put the swf
embedded in an HTML (assuming the file name is example.html):
<object type="application/x-shockwave-flash" data="meu-swf.swf"></object>
and run this HTML by a server like Apache, Ngnix or lighttpd accessing:
http://localhost/exemplo.html
A message will appear saying that the pop-up has been blocked, but then just release as requested by the browser to test.
Using POST with Urlloader.load
The navigationToURL
uses pop-up and this can be a problem, especially because of the blockers, I recommend doing the process using only Flash
and ActionScript
, in other words we will not open a new window, one way to do this is to use URLLoader.load
, code example (do not mix with other script):
var url:String = "http://localhost/post-test.php";
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.valor = encodeURIComponent("VALOR DE TESTE");
req.method = URLRequestMethod.POST;
req.contentType = "application/x-www-form-urlencoded";
req.data = variables;
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, completo, false, 0, true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioError, false, 0, true);
urlLoader.load(req);
function completo(e:Event):void
{
trace(urlLoader.data); // output: test
}
function ioError(e:IOErrorEvent):void
{
trace(e);
}
Note: The use of IOErrorEvent.IO_ERROR
is mandatory
To test this codes (both with load
as to navigateToURL
), you can use var_dump
or print_r
in your PHP, it would be a kind of simple and manual debugging, follow PHP:
<?php
echo 'GET:', PHP_EOL;
print_r($_GET);
echo 'POST:', PHP_EOL;
print_r($_POST);
Really?? So here you go. NO flash in the first frame I put the code as in the link above and then the php file that is on the same link, this: http://answall.com/questions/10273/enviar-vari%C3%A1veis-using-post Thanks Guilherme lol.
– dev
Just for the record, Sopt has several questions about how to pass data from JS to PHP, in the end it is practically the same thing. I think it’s worth a search.
– Bacco
I want to create the variable in flash, send it to php, open the php file and show the value of the variable (not easy) in the php page.
– dev
and Sopt is what?
– dev
@dev Stack Overflow in English... the search is there in the top corner. Anyway, I believe the posted answer solves your problem.
– Bacco