navigateToURL 通过 POST 将数据发送到 php 页面 [英] navigateToURL sending data via POST to php page

查看:35
本文介绍了navigateToURL 通过 POST 将数据发送到 php 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我在 Flash 应用程序中有一个表单,其中包含两个字段 input1 和 input2.当用户完成填写此表单时,它会转到一个 php 页面.目前,我正在使用 $_GET 方法发送数据.
像这样:

var request:URLRequest;request = new URLRequest("http://site.com/page.php?data1="+input1.text+"&data2="+input2.text);导航到URL(请求);

在php代码中:

$_GET["data1"];$_GET["data2"];

但是这样,信息就留在了 URL 中.如何通过 $_POST 发送?

解决方案

在 AS 3 中的 URLRequest 用于指定请求的类具有 method 属性,可用于设置提交方法的 HTTP 选项,您需要使用 URLRequestMethod 常量 POST 以获得完美的形式,或者您可以使用POST"字符串.>

您可以在 综合示例/" rel="nofollow">snipplr

简而言之:

var url:String = "http://localhost/myPostReceiver.php";var request:URLRequest = new URLRequest(url);var requestVars:URLVariables = new URLVariables();requestVars.foo = "bar";//... 填写你的数据request.data = requestVars;request.method = URLRequestMethod.POST;//在此之后使用 UrlLoader 或 navigateToUrl 加载您的 url

使用 Adob​​e Air 时,您需要使用 URLLoader 类而不是 navigateToURL() 因为以下花絮:

<块引用>

参数request:URLRequest — 一个 URLRequest 对象,指定要导航到的 URL.

对于在 Adob​​e AIR 中运行的内容,在使用 navigateToURL() 函数时,运行时会将使用 POST 方法(其方法属性设置为 URLRequestMethod.POST 的方法)的 URLRequest 视为使用 GET 方法.

基本上只要您想正确使用 POST 设置方法,正如 navigateToUrl:

接下来在 php 中你会收到超全局变量 $_POST 数组,您可以在其中访问它:

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?

解决方案

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

这篇关于navigateToURL 通过 POST 将数据发送到 php 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆